I have experimented with a few aproaches with regards to handling exceptions and came to conclusions that
rethrowing an exception is not a good idea. Why ? because you are loosing context information available in catch() clause. The exception being rethrown contains info about context in the lower layer of your application but it may not be enough to restore the whole sequence of event. For example, you are doing jdbc call and getting SQLException. You should not rethrow it because it doesn't contain all the information about parameters of the call. As soon as you leave catch(SQLEXception ex) block you gonna loose that "context". So, I ususaly have a dedicated Exception class for each layer. The constructor takes a Throwable from the lower layer and context from the current layer. This way you do not even have to log the exception on the lower layers.
think what happens with you application if all heap memory is gone ? Any "new" statement will throws a subclass of Error. So, any mission critical code must be surrounded by at least catch(Throwable ex). If you decide to put catch(Exception ex) you must have a very good reason why it is not Throwable... |