Exception Chaining
   HOME

TheInfoList



OR:

Exception chaining, or exception wrapping, is an
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
technique of handling exceptions by re-throwing a caught 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 doesn't need to know whether the error occurred during reading chunk of bytes or calling eof(). It needs only the exception message extracted from ''cause''. The user interface layer will have its own set of exceptions. The one interested in ''cause'' can see its stack trace during debugging or in proper log. Throwing the right kind of exceptions is particularly enforced by
checked exceptions In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
in the
Java programming language Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
, and starting with language version 1.4 almost all exceptions support chaining. In runtime engine environments such as Java or .NET there exist tools that attach to the runtime engine and every time that an exception of interest occurs they record debugging information that existed in memory at the time the exception was thrown (stack and heap values). These tools are called Exception Interception and they provide "root-cause" information for exceptions in Java programs that run in production, testing, or development environments.


References

* {{cite web , last1=Goetz , first1=Brian , date=2001-09-14 , df=mdy , url=https://www.infoworld.com/article/2075601/exceptional-practices--part-2.html , title=Exceptional practices, Part 2 , work=
JavaWorld ''InfoWorld'' (abbreviated IW) is an information technology media business. Founded in 1978, it began as a monthly magazine. In 2007, it transitioned to a web-only publication. Its parent company today is International Data Group, and its siste ...
, accessdate=2020-07-19
''Chained exceptions''
- Sun's Java tutorial Software design patterns