Share a flaw in Java exception implementation that you probably don't know about

foreword

A well-known knowledge point in Java is exception capture, try...catch...finally combination, but many people don't know that there is a defect in Java, or a shortcoming of exception implementation.

Here I will demonstrate the effect for you through a very simple experiment. I hope you will find it interesting.

simulation

1. Custom exceptions

Here, we first write a custom business exception, which is specially used to throw.

insert image description here

2. Simulation exception

Then, we write a test method to catch and throw a null pointer exception.

insert image description here

Look at the effect, OK no problem.

insert image description here

Next, let's add finally to see.

insert image description here

Look at the effect, OK is no problem.

insert image description here

Next we do this by throwing an exception in finally.

insert image description here

Looking at the effect, I found that the catch exception was covered.

insert image description here

Although this kind of scene is very special, it is actually a fly in the ointment in Java's implementation of exceptions, because exceptions should never be ignored as a sign of program errors, but exceptions are indeed lost in this scenario.

Next, let's test another situation, use return in finally, and see what happens.

insert image description here

Looking at the effect, I found that the exception caught in the catch simply disappeared, as if it had never come.

insert image description here

Finally, let's demonstrate another inexplicable thing you may have done or seen at work.

Let's modify this test method and look at the code.

insert image description here

Briefly describe, you call a query method of other classes, that method may habitually try...catch...finally, and finally some operations that must be executed at the end are performed. This business logic processing may have dozens of lines. You are likely to habitually make some judgments and throw exceptions.

Don't believe it, when a project enters the middle stage or even catches up with the progress, many people have already written the code mechanically and at a loss, or they may change the code on the basis of others, and you probably won't be too careful Line by line to see what is in those codes, it happens that there are no major problems during the test.

Then the result may be as follows. You will find that the thing that caught the exception of the query method that you deliberately threw at the beginning has no effect at all, and you don’t know where it went. You can’t find it no matter how you look. The following inexplicable anomaly, if you can't think of it later, forget it.

insert image description here

reason

This is actually a deficiency in the implementation of Java exceptions. Exceptions are a sign of program errors and should not be ignored, not to mention the commonly used behavior of finally, which directly or indirectly causes the loss of exceptions.

The author of "Thinking In Java" has clearly pointed out this exception and believes that this is a serious defect.是一个可能造成异常完全丢失的缺陷,而且是以一种更微妙、更难以察觉的方式在进行。

C++, on the other hand, handles it well and treats the situation where a second exception is thrown before the first one is handled as a serious programming error.

Summarize

After knowing this defect, it is actually easy to avoid it.

1. Avoid throwing exceptions in finally;

2. Avoid using return in finally;

3. In the catch, you must develop a good habit of recording exception logs with log.error, because the log will definitely be recorded, at least it will not leave you without clues.

At the end, I will demonstrate the effect of adding logs. As long as it is a catch, I will add logs, so I will not miss it.

insert image description here


The original article is purely hand-typed, typed out one by one, 键盘上全是血if you think it is helpful, please click 赞和收藏~

I am committed to sharing my work experience and interesting stories. If you like it, you can enter the homepage 关注一下~

Guess you like

Origin blog.csdn.net/xiangyangsanren/article/details/128211436