exceptions - The Stack Trace

本文深入探讨了Java中如何通过getStackTrace()方法获取异常堆栈信息,详细展示了如何在程序中使用该方法来捕获并打印堆栈跟踪,帮助开发者理解和定位程序中的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 The information provided by printStackTrace() can also be accessed directly using getStackTrace() .

// exceptions/WhoCalled.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Programmatic access to stack trace information

public class WhoCalled {
  static void f() {
    // Generate an exception to fill in the stack trace
    try {
      throw new Exception();
    } catch (Exception e) {
      for (StackTraceElement ste : e.getStackTrace()) {
        System.out.println(ste.getMethodName());
        System.out.println(ste);
      }

      // StackTraceElement[] stackTraceElements = e.getStackTrace();
      // int length = stackTraceElements.length;
      // for (int i = 0; i < length; i++) {
      //   System.out.println(stackTraceElements[i].getMethodName());
      // }
    }
  }

  static void g() {
    f();
  }

  static void h() {
    g();
  }

  public static void main(String[] args) {
    f();
    System.out.println("*******");
    g();
    System.out.println("*******");
    h();
  }
}
/* Output:
f
main
*******
f
g
main
*******
f
g
h
main
*/


/* My Output:
f
WhoCalled.f(WhoCalled.java:11)

main
WhoCalled.main(WhoCalled.java:29)
*******
f
WhoCalled.f(WhoCalled.java:11)
g
WhoCalled.g(WhoCalled.java:21)
main
WhoCalled.main(WhoCalled.java:31)
*******
f
WhoCalled.f(WhoCalled.java:11)
g
WhoCalled.g(WhoCalled.java:21)
h
WhoCalled.h(WhoCalled.java:25)
main
WhoCalled.main(WhoCalled.java:33)
*/
public StackTraceElement[] getStackTrace()

Provides programmatic access to the stack trace information printed by printStackTrace(). Returns an array of stack trace elements, each representing one stack frame. The zeroth element of the array (assuming the array's length is non-zero) represents the top of the stack, which is the last method invocation in the sequence. Typically, this is the point at which this throwable was created and thrown. The last element of the array (assuming the array's length is non-zero) represents the bottom of the stack, which is the first method invocation in the sequence.

Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace. Writes to the returned array do not affect future calls to this method.

Returns:

an array of stack trace elements representing the stack trace pertaining to this throwable.

Since:

1.4

 

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/WhoCalled.java

3. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#getStackTrace--

Execution failed for task ':app:mergeDebugResources'. > 8 exceptions were raised by workers: com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #0: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #1: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #2: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #3: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #4: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #5: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #6: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-osx Daemon #7: Daemon startup failed This should not happen under normal circumstances, please file an issue if it does. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
最新发布
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值