For .Net Framework 4.5 and above, you can use the Exception.HResult
property:
int hr = ex.HResult;
For older versions, you can use Marshal.GetHRForException
to get back the HResult, but this has significant side-effects and is not recommended:
int hr = Marshal.GetHRForException(ex);
More Related Contents:
- Catch multiple exceptions at once?
- How to handle AccessViolationException
- How slow are .NET exceptions?
- Cannot delete directory with Directory.Delete(path, true)
- What is the proper way to rethrow an exception in C#? [duplicate]
- Avoiding first chance exception messages when the exception is safely handled
- .NET – What’s the best way to implement a “catch all exceptions handler”
- Troubleshooting BadImageFormatException
- Does a locked object stay locked if an exception occurs inside it?
- Active Directory COM Exception – An operations error occurred (0x80072020)
- Why does a bad password cause “Padding is invalid and cannot be removed”?
- Is this a bad practice to catch a non-specific exception such as System.Exception? Why?
- How to avoid a System.Runtime.InteropServices.COMException?
- Image.FromStream() method returns Invalid Argument exception
- Read Big TXT File, Out of Memory Exception
- Best practices: throwing exceptions from properties
- How to prevent an exception in a background thread from terminating an application?
- Show line number in exception handling
- How to serialize an Exception object in C#?
- More Elegant Exception Handling Than Multiple Catch Blocks? [duplicate]
- ArgumentOutOfRangeException on initialized List
- How can I handle forms authentication timeout exceptions in ASP.NET?
- Exception.Message vs Exception.ToString()
- Is there a built in .NET exception that indicates an illegal object state?
- In a .net Exception how to get a stacktrace with argument values
- Which exceptions shouldn’t I catch?
- The ObjectContext instance has been disposed and can no longer be used for operations that require a connection [duplicate]
- Finding the cause of System.AccessViolationException
- Which types of exception not to catch?
- Dynamically changing Textbox’s AutoComplete List causes AccessViolationException, any advice?