Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling exceptions, is this a good way?

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?

Note: One of the drawbacks of this is that after the first catch you lose the ability to handle an specific exception (if you call a method that calls another method and the second one throws an exception you're not able to handle it) but I've found I've never done this any way ... I only handle exceptions with one level of depth ...

like image 913
Jorge Córdoba Avatar asked Mar 18 '10 12:03

Jorge Córdoba


People also ask

Is exception handling good?

Exception Handling: It's a Good Thing. There is nothing wrong with the previous code. But overusing these patterns will cause code smells, and won't necessarily be beneficial. Likewise, misusing them can actually do a lot of harm to your code base, making it brittle, or obfuscating the cause of errors.

Is exception good or bad?

Exceptions are not bad per se, but if you know they are going to happen a lot, they can be expensive in terms of performance. The rule of thumb is that exceptions should flag exceptional conditions, and that you should not use them for control of program flow. It also really depends on the language.

Is it a good approach to throw an exception?

The short answer is NO. You would throw an exception if the application can't continue executing with the bad data. In your example, the logic is to display an error message on the front end and Option 2 is the cleaner method for achieving this requirement.

Why is it important to use exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


1 Answers

If you log the exception too near the time it is first thrown, you won't be logging the full stack trace.

Handle exceptions (that is, fix them), as close as possible to when they were thrown. Gather information about the context as soon as possible to when they were thrown. But allow exceptions to propagate up to where they can actually be handled. Logging is a last-resort sort of handling, so it should occur in the outer layers of application subsystems.

This should eliminate the need for an application-specific exception used as a marker to not log an exception which shouldn't have been caught to begin with.

like image 119
John Saunders Avatar answered Oct 05 '22 04:10

John Saunders