Home  >  Article  >  Java  >  Detailed explanation of Java exception stack trace example code

Detailed explanation of Java exception stack trace example code

黄舟
黄舟Original
2017-03-17 10:51:331632browse

This article mainly introduces the detailed explanation of Java exception stack trace (Stack Trace) and related information of example code. Friends in need can refer to

Java exception stack trace (Stack Trace) Detailed explanation

When an exception is caught, some processing is often required. A simpler and more direct way is to print the exception stack trace Stack Trace. Speaking of stack traces, many people may be like me. The first reaction is the printStackTrace() method. In fact, in addition to this method, there are some other contents related to stack traces.

1.printStackTrace()

First of all, it needs to be clear that this method does not come from the Exception class. Except for the Exception class itself, which defines several constructors, all methods are inherited from its parent class. The methods related to exceptions are all inherited from the java.lang.Throwable class. And printStackTrace() is one of them.

This method will print the stack trace information of the Throwable object to the standard error output stream. The general appearance of the output is as follows:

java.lang.NullPointerException
     at MyClass.mash(MyClass.java:9)
     at MyClass.crunch(MyClass.java:6)
     at MyClass.main(MyClass.java:3)

The first line of output is the output of the toString() method, and the contents of the following lines are the contents previously saved through the fillInStackTrace() method. We will talk about this method later.

Let’s look at an example:

public class TestPrintStackTrace {
  public static void f() throws Exception{
    throw new Exception("出问题啦!");
  }
  public static void g() throws Exception{
    f();
  }
  public static void main(String[] args) {
    try {
      g();
    }catch(Exception e) {
      e.printStackTrace();
    }
  }
}

The output of this example is as follows:

java.lang.Exception: 出问题啦!
  at TestPrintStackTrace.f(TestPrintStackTrace.java:3)
  at TestPrintStackTrace.g(TestPrintStackTrace.java:6)
  at TestPrintStackTrace.main(TestPrintStackTrace.java:10)

In this example, an exception is thrown in method f(), call method f() in method g(), catch the exception in the main method, and print the stack trace information. Therefore, the output shows the process of f->g->main in sequence.

2.getStackTrace() method

This method provides programmatic access to the information printed by the printStackTrace() method. It returns an array of stack trace elements. Taking the above output as an example, the content of each line of output lines 2-4 corresponds to a stack trace element. Save these stack trace elements in an array. Each element corresponds to a stack frame of the stack. The first element of the array stores the top element of the stack, which is f above. The bottom element of the stack saved by the last element.

The following is an example of using getStackTrace() to access these trace stack elements and print the output:

public class TestPrintStackTrace {
  public static void f() throws Exception{
    throw new Exception("出问题啦!");
  }
  public static void g() throws Exception{
    f();
  }
  public static void main(String[] args) {
    try {
      g();
    }catch(Exception e) {
      e.printStackTrace();
      System.out.println("------------------------------");
      for(StackTraceElement elem : e.getStackTrace()) {
        System.out.println(elem);
      }
    }
  }
}

This output is basically the same as the output of printStackTrace(), as follows:

java.lang.Exception: 出问题啦!
  at TestPrintStackTrace.f(TestPrintStackTrace.java:3)
  at TestPrintStackTrace.g(TestPrintStackTrace.java:6)
  at TestPrintStackTrace.main(TestPrintStackTrace.java:10)
TestPrintStackTrace.f(TestPrintStackTrace.java:3)
TestPrintStackTrace.g(TestPrintStackTrace.java:6)
TestPrintStackTrace.main(TestPrintStackTrace.java:10)

3.fillInStackTrace()

We also mentioned this method before. To make this method clear, we must first talk about the problem of rethrowing exceptions after catching them. Catch the exception in the catch code block, print the stack trace, and throw it out again. In the previous level method call, catch this exception and print out the stack trace information. Will the two stack trace information be the same? Let’s take a look at the code:

public class TestPrintStackTrace {
  public static void f() throws Exception{
    throw new Exception("出问题啦!");
  }
  public static void g() throws Exception{
    try {
      f();
    }catch(Exception e) {
      e.printStackTrace();
      throw e;
    }
     
  }
  public static void main(String[] args) {
    try {
      g();
    }catch(Exception e) {
      e.printStackTrace();
    }
  }
}

The exception caught in the main method is thrown in the g() method. It stands to reason that the information printed by the two stack traces should be different. The information printed for the second time There should be no information about f. But in fact, the stack trace information printed twice is the same. The output result is as follows:

java.lang.Exception: 出问题啦!
  at TestPrintStackTrace.f(TestPrintStackTrace.java:3)
  at TestPrintStackTrace.g(TestPrintStackTrace.java:7)
  at TestPrintStackTrace.main(TestPrintStackTrace.java:16)
java.lang.Exception: 出问题啦!
  at TestPrintStackTrace.f(TestPrintStackTrace.java:3)
  at TestPrintStackTrace.g(TestPrintStackTrace.java:7)
  at TestPrintStackTrace.main(TestPrintStackTrace.java:16)

In other words, if the exception is caught and thrown immediately, and the exception is caught again in the superior method call, the printed stack trace information will be the same. The reason is that the state of the trajectory stack in the current state of the current thread is not saved into Throwabe. Now we introduce the fillInStackTrace() method. This method just does this kind of preservation work. Let’s take a look at the prototype of this method:

public Throwable fillInStackTrace()

This method has a return value. What is returned is a Throwable object that saves the current stack trace information. Let’s take a look at the difference in the stack trace information printed after using the fillInStackTrace() method. The code is as follows:

public class TestPrintStackTrace {
  public static void f() throws Exception{
    throw new Exception("出问题啦!");
  }
  public static void g() throws Exception{
    try {
      f();
    }catch(Exception e) {
      e.printStackTrace();
      //不要忘了强制类型转换
      throw (Exception)e.fillInStackTrace();
    }
     
  }
  public static void main(String[] args) {
    try {
      g();
    }catch(Exception e) {
      e.printStackTrace();
    }
  }
}

The output is as follows:

java.lang.Exception: 出问题啦!
  at TestPrintStackTrace.f(TestPrintStackTrace.java:3)
  at TestPrintStackTrace.g(TestPrintStackTrace.java:7)
  at TestPrintStackTrace.main(TestPrintStackTrace.java:17)
java.lang.Exception: 出问题啦!
  at TestPrintStackTrace.g(TestPrintStackTrace.java:11)
  at TestPrintStackTrace.main(TestPrintStackTrace.java:17)

We see that the stack is printed in the main method The trajectory no longer has f-related information.


The above is the detailed content of Detailed explanation of Java exception stack trace example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn