


How to locate the location where an exception occurs in the exception stack in java
The exception stack is the most important means for us to locate problems in daily life, and it provides us with great help in solving problems. But we may all have the habit of seeing an exception, especially when there are many exception stacks and deep levels.
I felt very worried and scared. I quickly scanned it and started to guess what the problem should be, and then continued to guess based on it. Adjusting the code, although debugging is also possible, still wastes a lot of time.
This is because:
1. We did not read the exception stack information carefully;
2. There are too many stacks. We're not sure exactly what's causing the problem.
The solution is:
1. Know the process of exception stack generation.
2. Read the stack information patiently.
3. Solve the problem
1. Exception generation process: The above error is reported, and the following follows
For example: We have the following test code
package com.bsx.test; public class TestException { public static void main(String[] args) { TestException exception = new TestException(); exception.m1(); } public void m1() { m2(); } public void m2() { m3(); } public void m3() { String name = null; System.out.println(name.length()); } }
After execution, the output result is as follows:
Exception in thread "main" java.lang.NullPointerException at com.bsx.test.TestException.m3(TestException.java:22) at com.bsx.test.TestException.m2(TestException.java:17) at com.bsx.test.TestException.m1(TestException.java:13) at com.bsx.test.TestException.main(TestException.java:9)
We can see that the order of the error log output is opposite to the calling order. Why?
We know that Java methods are executed in the virtual machine stack when executed. Each time a method is executed, a new stack frame will be created and pushed into the virtual machine stack.
This is a last-in-first-out structure, so when an error is reported, the error is reported from the callee first, and then the caller reports an error in sequence. Therefore, the order when printing errors is also that the error position is at the top, and the caller Row backwards. From this we can draw the conclusion: the error is reported above, and the following follows.
2. Understand the error message: Find the location where our code reports the error
From the above analysis, we know that the error location is above. In most cases, the error message at the top is where the error occurred in our code.
But sometimes the top log is not our own code. That is because our code calls the code of some third-party jar packages. But this does not affect us in locating the problem. We still locate the problem based on the error reported above and follow below. Then the real error location is still above.
Then we only need to find our own code from top to bottom. The first location of our code found is the location in our code that caused the error. Sometimes some error messages are obvious, and we can directly locate the crux of the problem based on the error messages.
Sometimes the error message does not clearly indicate the cause of the error. At this time, we can set a breakpoint at this precise location to debug it.
The above is the detailed content of How to locate the location where an exception occurs in the exception stack in java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),