Hiii.
Is there any way to uniquely identify a particular exception from the general exce开发者_开发百科ption class.
i.e any property or method that retrieves a unique identifier that to identify a particular exception.
I have kept some error values and corresponding error texts in an xml file. I want to read that xml file and have to taken the error ids and fix the corresponding texts to a label. in the case of a system exception we have to identify a particular exception.
Use Exception.HResult
.
' HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code. The severity code indicates whether the return value represents information, warning, or error. The facility code identifies the area of the system responsible for the error. The error code is a unique number that is assigned to represent the exception. Each exception is mapped to a distinct HRESULT. When managed code throws an exception, the runtime passes the HRESULT to the COM client. When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime. '
From http://msdn.microsoft.com/en-us/library/system.exception.hresult%28v=VS.100%29.aspx
UPDATE: the HResult field is protected on the System.Exception base class so you cannot access it. Derived COM classes make use of HResult as it was the primary way to report errors under COM, managed code does not use it.
I would suggest creating your own Exception type which contains a GUID:
public class MyExceptionWrapper : Exception
{
//Have a read on GUIDs and use one of the other constructors if possible.
public Guid GUID { get; set; }
//Construct your Exception type using the constructor from System.Exception
// and then assign the GUID.
public MyExceptionWrapper(string message, Exception inner) :
base(message, inner)
{
GUID = new Guid();
}
}
You can then use this pattern with error handling:
try
{
//cause an error
}
catch(Exception ex)
{
MyExceptionHandlerMethod(new MyExceptionWrapper("I caused an error", ex));
//OR - to throw up the stack
throw new MyExceptionWrapper("I caused an error", ex);
}
精彩评论