目录

Throwable fillInStackTrace()

描述 (Description)

java.lang.Throwable.fillInStackTrace()方法填充执行堆栈跟踪。 此方法在此Throwable对象中记录有关当前线程的堆栈帧的当前状态的信息。

声明 (Declaration)

以下是java.lang.Throwable.fillInStackTrace()方法的声明

public Throwable fillInStackTrace()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回对此Throwable实例的引用。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.Throwable.fillInStackTrace()方法的用法。

package com.iowiki;
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)
↑回到顶部↑
WIKI教程 @2018