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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。