Skip to content

Mini-primer on handling exceptions in .NET

There’s a lot of good technique in a short article. I wont repeat it all here, just a taste…

Code Sample #3:
try {
    // code
} catch (Exception e) {
   throw new ApplicationException(“Some useful message about what we’re doing: ” + e.Message);
}
 
Whoah again. We just lost a lot of information about the original exception, including its stack trace and any inner exceptions it contained and made our debugging lives a lot harder. I would recommend the following code instead:
 
try {
    // code
} catch (Exception e) {
   throw new ApplicationException(“Some useful message about what we’re doing”, e);
}
 
This code propagates the original exception as the innerException so that we can see the exact origin of the failure.

Courtesy of [James Kovacs’ Weblog]