Saturday, November 22, 2003

Exceptional Exceptions

Best Practices for Exception Handling "When deciding on checked exceptions vs. unchecked exceptions, ask yourself, "What action can the client code take when the exception occurs?"

If the client can take some alternate action to recover from the exception, make it a checked exception. If the client cannot do anything useful, then make the exception unchecked. By useful, I mean taking steps to recover from the exception and not just logging the exception."

"Preserve encapsulation.

Never let implementation-specific checked exceptions escalate to the higher layers. For example, do not propagate SQLException from data access code to the business objects layer."

The only one I slightly disagree with is:
"Log exceptions just once

Logging the same exception stack trace more than once can confuse the programmer examining the stack trace about the original source of exception. So just log it once. "

I think I'm right, in there's been cases where logging at different levels of abstraction have required multiple logging of the same base exception. I've also found it helpful rather than harmful.

No comments: