Handling exceptions, is this a good way?

Posted by Jorge Córdoba on Stack Overflow See other posts from Stack Overflow or by Jorge Córdoba
Published on 2010-03-18T12:39:07Z Indexed on 2010/03/18 12:41 UTC
Read the original article Hit count: 194

Filed under:
|

We're struggling with a policy to correctly handle exceptions in our application. Here's our goals for it (summarized):

  • Handle only specific exceptions.
  • Handle only exceptions that you can correct
  • Log only once.

We've come out with a solution that involves a generic Application Specific Exception and works like this in a piece of code:

try {
  // Do whatever
}
catch(ArgumentNullException ane)
{
  // Handle, optinally log and continue
}
catch(AppSpecificException)
{
  // Rethrow, don't log, don't do anything else
  throw;
}
catch(Exception e)
{
  // Log, encapsulate (so that it won't be logged again) and throw
  Logger.Log("Really bad thing", e.Message, e);
  throw new AppSpecificException(e)
}

All exception is logged and then turned to an AppSpecificException so that it won't be logged again. Eventually it will reach the last resort event handler that will deal with it if it has to.

I don't have so much experience with exception handling patterns... Is this a good way to solve our goals? Has it any major drawbacks or big red warnings?

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception-handling