Let Exceptions Propagate

EditHint: this is approaching the status of a DesignPattern. Perhaps refactor into one of the canonical PatternForms?

LetExceptionsPropagate is a call to let exceptions propagate higher up the call chain, rather than handling them right away.

The reasoning goes like so: Code nests. Frequently, the core decision making code isn't at the "grunt-work" level of outer function invocations. Rather, core decision making code lies in deeper, more fundamental levels of the call chain. The decision making code should be informed of failures, and decide what to do with them, not the grunt-work code.

Therefore: Let exceptions propagate higher up the call chain, rather than handling them right away.

Languages with Throw Clauses

In languages that support "throw clauses," maintaining throw clauses can become cumbersome. Read HomogenizeExceptions to learn about the problem and a solution to it.

-- originally by PhilGoodwin, reworked by LionKimbro

The pattern should be used in combination with ConvertExceptions and HomogenizeExceptions; see AvoidImplementationSpecificExceptions.

To use this pattern without HomogenizeExceptions, see LetExceptionsPropagateOnlyAsUncheckedExceptions


An interesting case: InterruptedException. For a thread to be interruptable, every long-running method it calls should declare InterruptedException. When a thread is interrupted, the exception propagates up to the thread's run() method, which halts (perhaps logging an error). In practice most interfaces don't declare this exception.

If a method cannot let InterruptedException propagate, a workaround is to call Thread.currentThread().interrupt() and continue. The interrupt will be handled the next time wait() or sleep() is called, which is hopefully responsive enough.

-- BrianSlesinsky

A thread can only be interrupted by an InterruptedException when it calls wait() or sleep(), so making a long running method declare InterruptedException will not automagically make it interruptable. Long running methods should call the Thread.interrupted() method to test if another thread wants to interrupt their activity.

In addition to wait() and sleep(), thread can be interrupted by InterruptedIOException whenever it calls a blocking I/O-operation.


See also: ThrowDontCatch, LetExceptionsPropagateOnlyAsUncheckedExceptions

CategoryException

View edit of February 10, 2005 or FindPage with title or text search