Treat Exception by code

Asked

Viewed 152 times

6

In Visual Basic 6 there was the command err.number where the error code is demonstrated.

I have a C# app that we treat Exception by the description of the message because I did not find the command that returns the error code. I have a problem because one of the machines has been changed the operating system to the message from Exception is returning in English and the treatment I have is in Portuguese.

Is there a command that returns the error code?

I found that there is one that displays the type of error, but from what I understand it may be a set of errors related to the type.

  • 1

    What kind of error does it refer to? Who generates it? There is not always an error with an associated number/code.

  • (...) that distinguishes it unequivocally.

2 answers

6


I believe you want the property HResult, it is the only form of code available in all exceptions. But honestly if you need this information, most likely you are doing something wrong, the exceptions themselves are sufficient to classify the error.

There are cases where the code may be relevant, perhaps the biggest example is SQLException, who owns the property ErrorCode that exactly catches the HRESULT more semantically. An example.

The operation of the exception in C# is different from VB error control, learn to use in the idiomatic way. Alias I suggest to study deeply the treatment of exceptions that is very often used mistakenly.

4

You can use the command:

int code = System.Runtime.InteropServices.Marshal.GetExceptionCode();

It will return the Exception code you seek

Browser other questions tagged

You are not signed in. Login or sign up in order to post.