This article describes the definition and usage of Java exception trace stack. Share it for your reference, as follows:
1. Introduction to the exception tracking stack
The printStackTrace method of the exception object is used to print the exception's trace stack information. According to the output result of the printStackTrace method, we can find the source of the exception and track the process of triggering the exception all the way.
2. Application of exception tracking stack in main method
1 Code Example
class SelfException extends RuntimeException{ SelfException(){} SelfException(String msg) { super(msg); }}public class PrintStackTraceTest{ public static void main(String[] args) { firstMethod(); } public static void firstMethod() { secondMethod(); } public static void secondMethod() { thirdMethod(); } public static void thirdMethod() { throw new SelfException("Custom Exception Information"); }}2 Running results
Exception in thread "main" SelfException: Custom exception information
at PrintStackTraceTest.thirdMethod(PrintStackTraceTest.java:26)
at PrintStackTraceTest.secondMethod(PrintStackTraceTest.java:22)
at PrintStackTraceTest.firstMethod(PrintStackTraceTest.java:18)
at PrintStackTraceTest.main(PrintStackTraceTest.java:14)
3 Results Analysis
As long as the exception is not completely caught, the exception gradually spreads outward from the method where the exception occurs, first passes to the caller of the method, and the method caller creates it again... until finally passes to the main method. If the main method still does not handle the exception, the JVM will abort the program and print the exception's trace stack information.
3. Application of exception tracking stack in multithread
1 Code Example
public class ThreadExceptionTest implements Runnable{ public void run() { firstMethod(); } public void firstMethod() { secondMethod(); } public void secondMethod() { int a = 5; int b = 0; int c = a / b; } public static void main(String[] args) { new Thread(new ThreadExceptionTest()).start(); }}2 Running results
Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
at ThreadExceptionTest.secondMethod(ThreadExceptionTest.java:16)
at ThreadExceptionTest.firstMethod(ThreadExceptionTest.java:10)
at ThreadExceptionTest.run(ThreadExceptionTest.java:6)
at java.lang.Thread.run(Thread.java:619)
3 Results Analysis
The program has an ArithmeticException exception in the run method of Thread. The source of this exception is the SecondMethod method of ThreadException, located in line 16 of the file. This exception will end when propagating to the run method of the Thread class.
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.