Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the type of Win32Exception?

Tags:

c#

.net

winapi

How do I get the type of Win32Exception, and classify it into something more specific? The message is only (arguably) good for showing it to the user.

  • Should I use Win32Exception.ErrorCode, Win32Exception.HResult, or Win32Exception.NativeErrorCode?
  • Are there any built-in enumerations for these codes?
like image 210
Andrey Moiseev Avatar asked Nov 23 '16 14:11

Andrey Moiseev


People also ask

What is Win32Exception 0x80004005?

Win32Exception (0x80004005): The system cannot find the path specified. Developer Network.

What is Win32Exception?

Win32 exceptions are typically exceptions mapped from errors returned from P/Invoked Windows API routines. To be able to debug them properly, you may need to check "Enable native debugging" in project and/or ide settings.

What is System ComponentModel Win32Exception?

ComponentModel. Win32Exception is the most basic exception type that will occur within your . NET applications when something goes wrong while using internal win32 -style operating system calls. These can vary from invalid path and file not found errors to network address issues and resource management problems.


1 Answers

It seems that to classify Win32Exception you should use use both:

  • Win32Exception.ErrorCode (HRESULT value)
  • Win32Exception.NativeErrorCode (system error code)

The exception message is a culture-specific translation of system error code, so don't use it. There are no built-in .NET classes for the mentioned values, you have to make your own.

Example: how to catch a specific "The system cannot find the file specified" kind of Win32Exception.

like image 112
Andrey Moiseev Avatar answered Oct 17 '22 03:10

Andrey Moiseev