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); } } } }
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)
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(); } } }
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)
public Throwable fillInStackTrace()
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(); } } }
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)
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!