How to say two .NET exceptions separately when they are of the same type?

When I come across:

View Certificate: .\SecurityTool.exe System.Security.Cryptography.CryptographicException: Cannot find the requested object. at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName) at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyS torageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName) at CORSIS.PortFusion.Security.Tool.view@167-1.Invoke (String file) in C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\P rogram.fs:line 170 > Private Key Password = "" = PS C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\bin\Debug> .\SecurityTool certificates view .\go.pfx CORSIS PortFusion : Distributed Reverse Proxy Security Tool 0.9.8.0 View Certificate: .\go.pfx System.Security.Cryptography.CryptographicException: The specified network password is not correct. at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySe t, SafeCertContextHandle& pCertCtx) at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyS torageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName) at CORSIS.PortFusion.Security.Tool.view@167-1.Invoke (String file) in C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\P rogram.fs:line 170 > Private Key Password = "" = PS C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\bin\Debug> 

How can I specify two instances of System.Security.Cryptography.CryptographicException separately?

Note: the exception texts are localized, so I can not compare them, because the descriptions will differ on German, Chinese machines, etc.

+4
source share
3 answers

I have not tested it, but the Exception.HResult property looks promising: MSDN

+2
source

You are right to not use message strings programmatically to distinguish exceptions. Your best rates for differentiating exceptions that use the same type are:

  • Data dictionary; this is often not used, but you can check to see if there are any data values ​​that differ between types.
  • Type InnerException (if any).

The HResult value HResult useful, but not available (this is a protected property), you will need to flip it to read it). It also has the same value for the same type of exception.

I should point out that more often than not, if two exceptions have the same type of runtime, they should usually be handled the same way. Evaluate if you really need to treat them differently.

+3
source

Their Message properties will be different.

0
source

Source: https://habr.com/ru/post/1388825/


All Articles