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

Just a note: I know that in Java finally block should never throw exceptions, otherwise this is [very, very, very] bad practice.
I know that I should use try-catch inside finally to handle (for example, logging or ignoring) all exceptions and prevent their propagation.
I know that Java 7 Throwable has a getSuppressed method, but I am targeting Java 5 and 6.

Problem: in Java, in try-finally , if an exception (name A) is thrown by the try block, the control reaches the finally block (in the absence of an exception, it also reaches, but this is not interesting in this matter). Then, if the finally block throws an exception (name B), exception A is suppressed or masked / swallowed, and exception B extends to the caller.

Question: Can I somehow determine the situation when one exception is suppressed by another and writes / writes the first?
... I spent too much time explaining why a particular exception was chosen, and not knowing what really happened.

Rationale: Often, problematic try-finally blocks are encoded in the library (today it was Hibernate), so I cannot modify them.

Limitations to the solution. As I noted at the beginning, a possible solution should not be relayed to Java 7, but, on the other hand, you do not need to be a production class (this will be a bonus). AOP is an option here.

(Please do not post trivial answers like "use Java 7 for development" :)

+4
source share
2 answers

The problem is, I think that even with AOP you cannot intercept the process of consuming exceptions. With AOP, you can capture, say, all the exceptions that were created, but you cannot know when they will be consumed.

For instance:

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

Not every exception is re-selected.

However, at a certain MOST level, an ARE exception was eventually thrown so that the caller could handle them.

So, given that you are most likely to "console" the exceptions, the game should try to find the "leaks" of the exceptions.

Using AOP, you can get away from each Exception as it is created (whether you can do it at the Exception level or do it at the level of a separate class, I can’t say - you are not familiar with AOP in practice in this case).

As soon as you throw exceptions when they are thrown, you start calling processing methods through AOP. When you do this, you can catch the exceptions thrown by the method.

So, with a little work, you know when you return from method a) which exceptions were thrown and b) which were thrown. If you throw an exception "caused" by the tree of the returned exception from the method, you can drop it from the list of "exceptions created by this method". The rest are leaks.

Ideally, after a little analysis, you can determine which ones are simply not thrown away (but handled in some other way) and which ones are actually obscured by your finally blocks.

This is imperfect, and, of course, I would not use it in production (I just would have to work a lot in all the conditions of the race around the record, etc. More work than I would like to do for this kind of debugging, to be honest). But it can give you the information you are looking for, especially if it is rarely used in a small class domain.

+1
source

There is a JNI method called "ExceptionCheck ()" which, unsurprisingly, checks for a pending exception. You can call it in the middle of your own method to find out if some previous call has raised an exception that expects the JNI method to return before it actually runs. I wonder if you can finally call your own method and then try to use ExceptionCheck to see if there is a pending exception, if that works. I have no idea if this will be, but I would say it is worth a try.

0
source

Source: https://habr.com/ru/post/1345954/


All Articles