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());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章