Exception chaining

This is an old revision of this page, as edited by AxelBoldt (talk | contribs) at 20:05, 4 February 2006 (AWB assisted spelling of "occurred"; see WP:Typo). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Exception chaining, or exception wrapping, is an object-oriented programming technique of handling exceptions by re-throwing a catched exception after wrapping it inside a new exception. The original exception is saved as a property (such as cause) of the new exception. The idea is that a method should throw exceptions defined at the same abstraction level as the method itself, but without discarding information from the lower levels.

For example, a method to play a movie file might handle exceptions in reading the file by re-throwing them inside an exception of movie playing. The user interface might display the time the error occurred extracted from the movie playing exception (movieplayingexception.time) and the name of the file extracted from the file reading exception (movieplayingexception.cause.filename).

Throwing the right kind of exceptions is particularily enforced by checked exceptions in the Java programming language, and starting with language version 1.4 all exceptions support chaining.

References