Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Nesting Exceptions

75% developed
From Wikibooks, open books for an open world
<Java Programming

Stack traceJava Programming
Nesting Exceptions
Concurrent Programming
NavigateExceptions topic:()


When an exception is caught, the exception contains thestack-trace, which describes the error and shows where the exception happened (i.e. where the problem is and where the application programmer should look to fix the problem). Sometimes it is desirable to catch an exception and throw another exception. If the new exception keeps a reference to the first exception, the first exception is called anesting exception.

Computer codeCode listing 6.4: NestingExceptionExample.java
publicclassNestingExceptionExample{publicstaticvoidmain(String[]args)throwsException{Object[]localArgs=(Object[])args;try{Integer[]numbers=(Integer[])localArgs;}catch(ClassCastExceptionoriginalException){ExceptiongeneralException=newException("Horrible exception!",originalException);throwgeneralException;}}}
Standard input or outputOutput for Code listing 6.4
Exception in thread "main" java.lang.Exception: Horrible exception!at NestingExceptionExample.main(NestingExceptionExample.java:9)Caused by: java.lang.ClassCastException: [Ljava.lang.String; incompatible with [Ljava.lang.Integer;at NestingExceptionExample.main(NestingExceptionExample.java:7)

The above code is an example of a nesting exception. When theException is thrown, by passing in theClassCastException object reference as a parameter, theClassCastException is nested in the newly createdException, its stack-trace is appended together. When theException is caught, its stack-trace contains the originalClassCastException's stack-trace.

This is a kind of exception conversion, from one exception to another. For example, calling a remote object using RMI, the calling method has to deal withRemoteException which is thrown if something is wrong during the communication. From the application point of view,RemoteException has no meaning, it should be transparent to the application that a remote object was used or not. So theRemoteException should be converted to an application exception.

This conversion can also hide where the error is originated. The stack-trace starts when the exception is thrown. So when we catch and throw a new exception, the stack-trace starts at when the new exception was thrown, losing the original stack-trace. This was true with the earlier version of Java (before 1.4). Since then, so calledcause facility capabilities were built in theThrowable class.

A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Finally, it can contain a cause: another throwable that caused this throwable to get thrown. The cause facility is also known as thechained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.

A cause can be associated with a throwable in two ways: via a constructor that takes the cause as an argument, or via theinitCause(Throwable) method. New throwable classes that wish to allow causes to be associated with them should provide constructors that take a cause and delegate (perhaps indirectly) to one of theThrowable constructors that takes a cause. For example:

ExampleCode section 6.26: Chaining-aware constructor.
try{lowLevelOp();}catch(LowLevelExceptionle){thrownewHighLevelException(le);}

Because the initCause method is public, it allows a cause to be associated with any throwable, even a "legacy throwable" whose implementation predates the addition of the exception chaining mechanism to Throwable. For example:

ExampleCode section 6.27: Legacy constructor.
try{lowLevelOp();}catch(LowLevelExceptionle){throw(HighLevelException)newHighLevelException().initCause(le);}

Further, as of release 1.4, many general purpose Throwable classes (for exampleException,RuntimeException,Error) have been retrofitted with constructors that take a cause. This was not strictly necessary, due to the existence of theinitCause method, but it is more convenient and expressive to delegate to a constructor that takes a cause.

By convention, classThrowable and its subclasses have two constructors, one that takes no arguments and one that takes a String argument that can be used to produce a detail message. Further, those subclasses that might likely have a cause associated with them should have two more constructors, one that takes aThrowable (the cause), and one that takes a String (the detail message) and aThrowable (the cause).


Clipboard

To do:
Add some exercises like the ones inVariables


Stack traceJava Programming
Nesting Exceptions
Concurrent Programming
Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Nesting_Exceptions&oldid=3644636"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp