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);
Related Contents:
- Is there a difference between “throw” and “throw ex”?
- How to handle AccessViolationException
- WPF global exception handler [duplicate]
- What is the proper way to rethrow an exception in C#? [duplicate]
- Catch multiple exceptions at once?
- How slow are .NET exceptions?
- TAP global exception handler
- Exception messages in English?
- How using try catch for exception handling is best practice
- Cannot delete directory with Directory.Delete(path, true)
- How to rethrow InnerException without losing stack trace in C#?
- Puzzling Enumerable.Cast InvalidCastException
- Can you catch a native exception in C# code?
- Troubleshooting BadImageFormatException
- Avoiding first chance exception messages when the exception is safely handled
- .NET – What’s the best way to implement a “catch all exceptions handler”
- How to serialize an Exception object in C#?
- Does a locked object stay locked if an exception occurs inside it?
- Why does a bad password cause “Padding is invalid and cannot be removed”?
- Active Directory COM Exception – An operations error occurred (0x80072020)
- User Activity Logging, Telemetry (and Variables in Global Exception Handlers)
- Avoiding null reference exceptions
- When to use try/catch blocks?
- How to check if IOException is Not-Enough-Disk-Space-Exception type?
- Is this a bad practice to catch a non-specific exception such as System.Exception? Why?
- Exception.Message vs Exception.ToString()
- InvalidProgramException / Common Language Runtime detected an invalid program
- How to log all thrown exceptions?
- Read Big TXT File, Out of Memory Exception
- Image.FromStream() method returns Invalid Argument exception
- What is ApplicationException for in .NET?
- Best practices: throwing exceptions from properties
- More Elegant Exception Handling Than Multiple Catch Blocks? [duplicate]
- How to check for valid xml in string input before calling .LoadXml()
- Is there a built in .NET exception that indicates an illegal object state?
- Why Create Custom Exceptions? [closed]
- Why can’t I catch a generic exception in C#?
- How to get line number(s) in the StackTrace of an exception thrown in .NET to show up
- Reading CSV file and storing values into an array
- Storing WPF Image Resources
- Reading a C/C++ data structure in C# from a byte array
- In MVVM should the ViewModel or Model implement INotifyPropertyChanged?
- Build query string for System.Net.HttpClient get
- How can I get all constants of a type by reflection?
- How to open an Excel file in C#?
- How to deploy application with sql server database on clients
- Unhandled exceptions in BackgroundWorker
- Is the Linq Count() faster or slower than List.Count or Array.Length?
- How to use LogonUser properly to impersonate domain user from workgroup client
- How to list available instances of SQL Servers using SMO in C#?