java.lang.Throwable.fillInStackTrace()方法實例
java.lang.Throwable.fillInStackTrace() 方法填充在執行堆棧跟蹤。此方法記錄該Throwable對象信息內有關當前線程的堆棧幀的當前狀態。
聲明
以下是java.lang.Throwable.fillInStackTrace()方法的聲明
public Throwable fillInStackTrace()
參數
-
NA
返回值
該方法返回一個引用該Throwable的實例。
異常
-
NA
例子
下麵的例子顯示了java.lang.Throwable.fillInStackTrace()方法的使用。
package com.yiibai; import java.lang.*; public class ThrowableDemo { public static void function1() throws Exception { throw new Exception("this is thrown from function1()"); } public static void function2() throws Throwable { try { function1(); } catch(Exception e) { System.err.println("Inside function2():"); e.printStackTrace(); throw e.fillInStackTrace(); } } public static void main(String[] args) throws Throwable { try { function2(); } catch (Exception e) { System.err.println("Caught Inside Main:"); e.printStackTrace(); } } }
讓我們來編譯和運行上麵的程序,這將產生以下結果:
Inside function2(): java.lang.Exception: this is thrown from function1() at ThrowableDemo.function1(ThrowableDemo.java:4) at ThrowableDemo.function2(ThrowableDemo.java:9) at ThrowableDemo.main(ThrowableDemo.java:19) Caught Inside Main: java.lang.Exception: this is thrown from function1() at ThrowableDemo.function2(ThrowableDemo.java:13) at ThrowableDemo.main(ThrowableDemo.java:19)