📜  Java中的可抛出 getStackTrace() 方法及示例

📅  最后修改于: 2022-05-13 01:55:18.143000             🧑  作者: Mango

Java中的可抛出 getStackTrace() 方法及示例

Throwable 类getStackTrace()方法用于返回堆栈跟踪元素数组,该数组是 printStackTrace() 打印的堆栈跟踪信息。在堆栈跟踪元素数组中(假设数组的长度不为零),每个元素代表一个堆栈帧。数组的第一个元素表示该数组的第零个索引元素表示堆栈的顶部,这是序列中调用的最后一个方法,或者我们可以说这个第零个索引元素信息与创建和抛出 throwable 的点有关.该数组的最后一个元素表示堆栈的底部,这是序列中调用的第一个方法。
在某些情况下,返回堆栈跟踪中的一个或多个堆栈帧。此方法返回的数组将为 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 Java()