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


Java Throwable getStackTrace()用法及代碼示例


Throwable類的getStackTrace()方法用於返回堆棧跟蹤元素數組,該數組是printStackTrace()打印的堆棧跟蹤信息。在堆棧跟蹤元素數組中(假設數組的長度不為零),每個元素代表一個堆棧幀。數組的第一個元素表示此數組的第零個索引元素代表堆棧的頂部,這是序列中最後調用的方法,或者我們可以說該第零個索引元素信息與創建和拋出可拋出事件的點有關。該數組的最後一個元素表示堆棧的底部,這是序列中調用的第一個方法。
在某些情況下,將返回堆棧跟蹤中的一個或多個堆棧幀。此方法返回的數組將由printStackTrace打印的每一幀包含一個元素。對返回數組的任何更改都不會影響以後對該方法的調用。

用法:

public StackTraceElement[] getStackTrace()

返回值:此方法返回表示堆棧跟蹤信息的堆棧跟蹤元素數組。


下麵的程序說明Throwable類的getStackTrace方法:

示例1:

// Java program to demonstrate 
// the getStackTrace() Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            // add the numbers 
            addPositiveNumbers(2, -1); 
        } 
  
        catch (Throwable e) { 
  
            // get StackTraceElements 
            // using getStackTrace() 
            StackTraceElement[] stktrace 
                = e.getStackTrace(); 
  
            // print element of stktrace 
            for (int i = 0; i < stktrace.length; i++) { 
  
                System.out.println("Index " + i 
                                   + " of stack trace"
                                   + " array conatins = "
                                   + stktrace[i].toString()); 
            } 
        } 
    } 
  
    // method which adds two positive number 
    public static void addPositiveNumbers(int a, int b) 
        throws Exception 
    { 
  
        // if Numbers are Positive 
        // than add or throw Exception 
        if (a < 0 || b < 0) { 
  
            throw new Exception( 
                "Numbers are not Positive"); 
        } 
  
        else { 
  
            System.out.println(a + b); 
        } 
    } 
}
輸出:
Index 0 of stack trace array conatins = GFG.addPositiveNumbers(File.java:48)
Index 1 of stack trace array conatins = GFG.main(File.java:18)

示例2:

// Java program to demonstrate 
// the getStackTrace() Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            // get StackTraceElements 
            // using getStackTrace() 
            StackTraceElement[] stktrace 
                = e.getStackTrace(); 
  
            // print element of stktrace 
            for (int i = 0; i < stktrace.length; i++) { 
  
                System.out.println("Index " + i 
                                   + " of stack trace"
                                   + " array conatins = "
                                   + stktrace[i].toString()); 
            } 
        } 
    } 
  
    // method which throws Exception 
    // calling other method testException2 
    public static void testException1() 
        throws Exception 
    { 
        // This method second in series 
        // of calling method which throw exception 
        // so this will be second index element 
        testException2(); 
    } 
  
    // method which throws Exception 
    // calling other method testException3 
    public static void testException2() 
        throws Exception 
    { 
  
        // This method calls a method 
        // where exception is thrown 
        // so this will be first index element 
        testException3(); 
    } 
  
    // method which throws IndexOutOfBoundsException 
    public static void testException3() 
        throws IndexOutOfBoundsException 
    { 
  
        // here exception thrown 
        // so this will be Zeroth element 
        throw new IndexOutOfBoundsException( 
            "Forcefully Generated Exception"); 
    } 
}
輸出:
Index 0 of stack trace array conatins = GFG.testException3(File.java:68)
Index 1 of stack trace array conatins = GFG.testException2(File.java:58)
Index 2 of stack trace array conatins = GFG.testException1(File.java:46)
Index 3 of stack trace array conatins = GFG.main(File.java:17)

參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getStackTrace()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Throwable getStackTrace() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。