How to record exceptions masked by finally block exceptions in Java?

Please note that:

Problem: in Java, in try finally, if the try block throws an exception (named a), the control will reach the finally block (if there is no exception, it will also reach it, but it has no meaning in this problem) Then, if the finally block throws an exception (named B), exception a is suppressed or masked / swallowed, and exception B is propagated to the caller

Question: when an exception is compressed by another exception and the first exception is recorded / recorded, can I detect this in some way I spent too much time reasoning why I threw a special exception and didn't know what happened

Reason: the try finally blocks with problems are usually encoded in the library (today it is hibernate), so I can't modify them

Solution limitations: as I pointed out at the beginning, possible solutions should not be forwarded on Java 7, but on the other hand, they do not need to be production level (this will be a reward) AOP is the choice here

(please don't publish trivial answers like "developing with Java 7":)

Solution

I think the problem is that even with AOP, you can't intercept abnormal consumption processes With AOP, you can catch all created exceptions, but you don't know when they are consumed

For example:

try {
    ...
} catch (Exception e) {
    log.boom("Ouchies happened here",e);
}

Not every exception is thrown again

At some level, however, most exceptions are eventually thrown so that the caller can handle them

So, given that you may "leak" exceptions, the game is trying to find "leak" exceptions

With AOP, you can eliminate every exception when you create it (whether you do it at the exception level or you have to do it at a single level, I can't say – you're not familiar with AOP in practice)

Once you catch an exception when you create it, you start wrapping method calls through AOP When you do this, you can catch the exception thrown by the method

So, with some work, you know what exceptions are a) created and b) thrown when you return from the method If you traverse the exceptions caused by... That return exceptions from the method, you can remove these exceptions from the exceptions created by this method list The rest are loopholes

Ideally, after some analysis, you will be able to identify which are not thrown (but otherwise handled) and which are actually obscured by your finally block

It's not perfect, and I certainly won't use it in production (I just need to put a lot of work into all the competitive conditions such as recording. More debugging than I want to do, frankly) However, it may provide you with the information you are looking for, especially when used with caution in a small range of courses

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>