當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。