4

Is there a way that i can get the corresponding error code of an Exceptions ? I need the thrown exceptions error code instead of its message , so that i based on the error code i show the right message to the user.

Hossein
  • 24,202
  • 35
  • 119
  • 224

3 Answers3

8

If you're looking for the win32 error code, that's available on the Win32Exception class

catch (Win32Exception e)
{  
    Console.WriteLine("ErrorCode: {0}", e.ErrorCode);
}

For plain old CLR exception, there is no integer error code.

Given the problem you describe, I'd go with millimoose's solution for getting resource strings for each type of exception.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
5

The whole point of exceptions is that they provide richer information than just an error code. By default, they don't have one, and don't really need one. If you like using error codes you can just use your own exception base class that you derive all your exceptions from:

public abstract class MyExceptionBase : Exception 
{
    public int ErrorCode { get; set; }
    // ...
}

That said, I wouldn't bother. Personally I map exceptions to error messages using their type name:

ResourceManager errorMessages = ...;
errorMessages.GetString(ex.GetType().FullName);

(You can also create more flexible schemes, like make the resources format strings and interpolate exception properties into them.)

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • +1 This is pretty much what I do as well. – p.s.w.g Mar 17 '13 at 15:54
  • Will "ex.GetType().FullName" always return the same text (e.g. "System.Security.SecurityException") no matter the language of the OS or other local settings? – Rado Aug 09 '16 at 05:57
  • @Rado It's the type name in code, `.GetType().FullName` is standard .NET reflection, not an exception-specific API. I'm about 99.999% certain it's impossible to localize type names in C#. – millimoose Aug 14 '16 at 02:57
  • @millimoose Thank you! I'm just looking for a way to determine with certainty the type of error message so since there apparently are no error codes I guess string comparison of the error message is the only way to do it. – Rado Aug 16 '16 at 06:11
  • @Rado Well, you could also use something like [`Type.AssemblyQualifiedName`](https://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname(v=vs.110).aspx) to be completely unambiguous, but that's probably overkill for error classification. – millimoose Aug 16 '16 at 12:56
  • @Rado Also, my answer isn't about comparing the text of the error *message*. It's about using the name of the error's *exception* type in the codebase. Unless its author did something stupid, like use one exception for all errors. – millimoose Aug 16 '16 at 12:58
  • @millimoose Yes, I understand what you mean just didn't express myself correctly - I know from experience that the error message may be in the local language so that's not useful for string comparison. Thanks again... – Rado Aug 17 '16 at 16:11
3

For a COM exception that is upgraded to a Managed exception, you will be able to retrieve the "error code" from the HResult property as such:

try {
    // code goes here
} catch(System.IO.FileNotFoundException ex) {
    Console.WriteLine(
        String.Format("(HRESULT:0x{1:X8}) {0}",
                      ex.Message,
                      ex.HResult)
    );
}

Not all exceptions however will have a meaningful HResult set however.


For .NET 3.0, 3.5 and 4.0 you will have to use reflection to get the value of the HResult property as it is marked protected.

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175