Chained Exceptions

Chained Exceptions

An application often responds to an exception by throwing another exception. In effect, the first exception causes the second exception. It can be very helpful to know when one exception causes another. Chained Exceptions help the programmer do this.

程序经常为了响应某个异常而抛出另外一个异常。事实上,第一个异常导致了第二个异常。知道什么时候一个异常导致了另外一个异常是非常有帮助的。链式异常帮助程序员做到这点。

The following are the methods and constructors in Throwable that support chained exceptions.

下面的是Throwable类中的方法和构造器,支持链式异常。

Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
Throwable(Throwable)

The Throwable argument to initCause and the Throwable constructors is the exception that caused the current exception. getCause returns the exception that caused the current exception, and initCause sets the current exception’s cause.

initCause方法和Throwable构造器的Throwable参数是导致当前异常的一个异常。getCause方法返回导致当前异常的那个异常,initCause方法设置当前异常的原因。

The following example shows how to use a chained exception.

下面的例子展示了如何去使用链式异常。

try {

} catch (IOException e) {
    throw new SampleException("Other IOException", e);
}

In this example, when an IOException is caught, a new SampleException exception is created with the original cause attached and the chain of exceptions is thrown up to the next higher level exception handler.

在这个例子中,当一个IOException被捕获时,一个新的SampleException异常被创建,原来的异常被附加新的异常上,异常链被抛出到达下一个更高级别的异常处理器。

Accessing Stack Trace Information

Now let’s suppose that the higher-level exception handler wants to dump the stack trace in its own format.

现在让我们假设,这个更高级别的异常处理器想以它自己的格式转存堆栈轨迹。

Definition: A stack trace provides information on the execution history of the current thread and lists the names of the classes and methods that were called at the point when the exception occurred. A stack trace is a useful debugging tool that you’ll normally take advantage of when an exception has been thrown.

定义:堆栈轨迹提供了当前线程上执行的历史信息,并列出了在异常出现的这个时候被调用的类和方法的名称。堆栈轨迹是一个很有用的调试工具,当一个异常被抛出时你将通常去利用它。

The following code shows how to call the getStackTrace method on the exception object.

下面的代码展示如何去调用异常对象的getStackTrace方法。

catch (Exception cause) {
    StackTraceElement elements[] = cause.getStackTrace();
    for (int i = 0, n = elements.length; i < n; i++) {       
        System.err.println(elements[i].getFileName()
            + ":" + elements[i].getLineNumber() 
            + ">> "
            + elements[i].getMethodName() + "()");
    }
}

Logging API

The next code snippet logs where an exception occurred from within the catch block. However, rather than manually parsing the stack trace and sending the output to System.err(), it sends the output to a file using the logging facility in the java.util.logging package.

下一个代码片段中,在catch代码块中记录了异常是从哪里出现的。它使用java.util.logging包中的日志功能将输出发送到一个文件,而不是手动的解析堆栈轨迹和将输出发送到System.err()

try {
    Handler handler = new FileHandler("OutFile.log");
    Logger.getLogger("").addHandler(handler);

} catch (IOException e) {
    Logger logger = Logger.getLogger("package.name"); 
    StackTraceElement elements[] = e.getStackTrace();
    for (int i = 0, n = elements.length; i < n; i++) {
        logger.log(Level.WARNING, elements[i].getMethodName());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章