December 2007
Intermediate to advanced
896 pages
19h 57m
English
You have an exception that may have multiple nested inner exceptions, and you need a quick way to analyze them for common or well-known errors.
Use the GetNestedExceptionList extension method provided here with any Exception to retrieve an IEnumerable<Exception> list of all of the exceptions in the inner exception tree. Once this information is in list format, use Language Integrated Query (LINQ) to isolate common issues:
public static IEnumerable<Exception> GetNestedExceptionList(this Exception exception)
{
Exception current = exception;
do
{
current = current.InnerException;
if (current != null)
yield return current;
}
while (current != null);
}An example of how this can be used in conjunction with LINQ to find well-known exceptions is listed below:
// simulate an exception chain unwinding // hit a bad reference NullReferenceException nrex = new NullReferenceException("Touched a bad object"); nrex.Data.Add("Bad ObjectID", "0x34874573"); // have an error formatting a message for it FormatException fex = new FormatException("Resulted from Null Reference", nrex); // mask it as part of a data layer ApplicationException appex = new ApplicationException("There was an error in the data layer.",fex); // hit another error reformatting the exception information FormatException fmtEx = new FormatException("Error formatting error message.", appex); // Use LINQ to look for common or well-known problems in the app var query = from ex ...Read now
Unlock full access