当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Throwable fillInStackTrace()用法及代码示例


java.lang.Throwable类的fillInStackTrace()方法在此Throwable对象中记录有关当前线程的堆栈帧的当前状态的信息。这意味着使用此方法可以看到类的当前方法的异常消息,其中调用了fillInStackTrace()方法。如果还有其他消息可以从当前方法派生而抛出异常,则可以跳过这些其他消息详细信息。

用法:

public Throwable fillInStackTrace()

返回值:此方法返回对其上应用了fillInStackTrace()的Throwable对象的引用。


以下示例程序旨在说明Method类的fillInStackTrace()方法:

示例1:该程序显示如果不使用fillInStackTrace()方法将显示什么结果,如果不使用fillInStackTrace()方法将发生什么结果

说明:使用fillInStackTrace()仅返回当前线程的帧的活动状态信息。因此,当调用fillInStackTrace()时,该方法将详细信息返回到调用fillInStackTrace()方法的主方法。

// Java program to demonstrate 
// fillInStackTrace() method 
  
public class GFG { 
    // Main Method 
    public static void main(String[] args) throws Throwable 
    { 
        GFG gfg = new GFG(); 
        try { 
            // calling this method will throw exception 
            gfg.method(); 
        } 
        catch (Exception e) { 
  
            // Exception details without using fillInStackTrace() 
  
            System.out.println("Exception details without fillInStackTrace()\n"); 
            System.err.println("Caught Inside Main:"); 
            e.printStackTrace(); 
  
            // Exception details using fillInStackTrace() 
  
            System.out.println("Exception details with fillInStackTrace()\n"); 
            System.err.println("Caught Inside Main:"); 
            e.fillInStackTrace(); 
            e.printStackTrace(); 
        } 
    } 
  
    // method calling divide operation 
    public void method() throws Throwable 
    { 
        divide(); 
    } 
  
    // divide operation throws ArithmeticException exception 
    void divide() 
    { 
  
        try { 
            System.out.println(10 / 0); 
        } 
        catch (ArithmeticException e) { 
            throw e; 
        } 
    } 
}

输出:

Exception details without fillInStackTrace()

Caught Inside Main:
java.lang.ArithmeticException: / by zero
    at GFG.divide(GFG.java:38)
    at GFG.method(GFG.java:31)
    at GFG.main(GFG.java:13)

Exception details with fillInStackTrace()

Caught Inside Main:
java.lang.ArithmeticException: / by zero
    at GFG.main(GFG.java:23)

示例2:应用fillInStackTrace()后,该程序将打印详细信息。

说明:使用fillInStackTrace()仅返回当前线程的帧的活动状态信息。因此,当调用fillInStackTrace()时,该方法返回异常详细信息,直到showResults方法为止,在该方法中调用了fillInStackTrace()方法。但是main()方法显示了整个异常的详细信息,因为在主方法中未调用fillInStackTrace()。

// Java program to demonstrate 
// fillInStackTrace() method 
  
public class GFG { 
  
    // Main Method 
    public static void main(String[] args) throws Throwable 
    { 
        GFG gfg = new GFG(); 
        try { 
            // calling this method will throw an exception 
            gfg.showResults(); 
        } 
        catch (Exception e) { 
  
            // Exception details using fillInStackTrace() 
            e.printStackTrace(); 
        } 
    } 
  
    // method calling exceptionThrownMethod() 
    // and when method returns Exception 
    // it is calling fillInStackTrace() method 
    public void showResults() throws Throwable 
    { 
        try { 
            exceptionThrownMethod(); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
            throw e.fillInStackTrace(); 
        } 
    } 
  
    // method throwing exception 
    public void exceptionThrownMethod() throws Exception 
    { 
        throw new Exception("this is thrown from function1()"); 
    } 
}

输出:

java.lang.Exception: this is thrown from function1()
    at GFG.exceptionThrownMethod(GFG.java:35)
    at GFG.showResults(GFG.java:27)
    at GFG.main(GFG.java:13)

java.lang.Exception: this is thrown from function1()
    at GFG.showResults(GFG.java:30)
    at GFG.main(GFG.java:13)

参考: https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Throwable fillInStackTrace() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。