I have an app that runs a long batch process where many exceptions could potentially be thrown. If a non-critical exception is thrown during one item in the batch, I want to simply log it and continue, so we can fix the problem later while letting the other batch items continue.
Some exceptions, such as OutOfMemoryException, are devastating to the app as a whole, and these I would like to rethrow so that they bubble up to global exception handler which will log the error and stop the app.
So my question is, is there a reasonbly short list of critical exceptions that I can rethrow in my lower exception handler while suppressing (after logging) everything else?
Thanks!
Edit: To elaborate a little, here is the basic structure of my program
foreach(var item in longItemList)
{
try
{
bigDynamicDispatchMethod(item);
}
catch(Exception ex)
{
logException(ex);
}
}
There are potentially a huge number of exceptions that could be thrown because this loop is pretty much at the top level of my app. 99% of the code in my project is behind the dispatch method. I do reasonable exception handling at lower levels, but bugs still work their way in and I don’t want to stop other unrelated processes in the batch after an exception is thrown.
Trying to find which exceptions could be thrown everywhere else in my app seems like a daunting task, and it seemed to be that it would be simpler to get a blacklist of critical exceptions.
Is there a better way to structure my app to deal with this? I am open to suggestions.
You don’t need a list of ‘bad’ exceptions, you should treat everything as bad by default. Only catch what you can handle and recover from. CLR can notify you of unhandled exceptions so that you can log them appropriately. Swallowing everything but a black listed exceptions is not a proper way to fix your bugs. That would just mask them. Read this and this.
Few other rules:
Eric Lippert classifies all exceptions into 4 groups: Fatal, ‘Boneheaded’, Vexing, Exogenous. Following is my interpretation of Eric’s advice:
This is roughly equivalent to Microsoft’s categorization: Usage, Program error and System failure.
You can also use static analysis tools like FxCop to enforce some of these rules.