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.tutorialspoint;
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)

