Mask Exceptions Appropriately

Perhaps this should be merged with TranslateExceptions and/or other related pages (I did this once, and the original author ignored the merge and restored this page)


Context: You LetExceptionsPropagate.

Problem: You are getting similar exceptions from all over. You don't know what went wrong because all exceptions are simply Exception or NullPointerException or something similar.

Solution: MaskExceptionsAppropriately where they are thrown or rethrown.

This is similar to ConvertExceptions, without hiding where the original exception was thrown. The original exception must be wrapped into the new exception; see NestedException to know how. The idea here is that you should create new exceptions that closely and explicitly indicate what went wrong, as is explained in RefineExceptions.

DontThrowGenericExceptions. Also avoid ExceptionTunneling. GenericExceptions? are considered bad style. Whenever a generic exception is thrown, you should mask it into a more meaningful exception.

For example: NullPointerException should be mapped to a meaningful exception like ConnectionException? at the database level or to TransactionException? at the application level.

HomogenizeExceptions by doing the following: ConvertExceptions at package boundaries so that they make sense.

Now when catching exceptions, try to be as general as possible. Possible in this context means useful and meaningful. Catching Exception all the time is also bad style. See HomogenizeExceptions.


See also AvoidImplementationSpecificExceptions.


CategoryException


EditText of this page (last edited June 11, 2005) or FindPage with title or text search