Receive notifications about each exception (even processed)

Is there a way to get notified of all exceptions that occur at runtime (processed and not processed)?

What I want is a log mechanism that logs every exception that appears during my program. I do not want to handle exceptions with this registrar, I just want to be able to register the event of the generated exception.
The fact is that I also want to include all system exceptions, so it is impossible to call a function whenever I throw a new exception ...

I read something about the ExceptionListener , but they seem to be for a different job.

Does anyone know how this is possible?

+5
source share
1 answer

Exceptions are usually recorded during capture (and not thrown), so their consequences can also be recorded. That is, some exceptions have no consequences, therefore they do not require investigation, while other exceptions are extremely important and should be investigated whenever they occur. Therefore, a good log file should contain not only what the exception occurred, but also the impact (business) of this exception, which is known only after the exception is caught. This requires that exceptions be logged when they are processed.

Therefore, there is no Java API to catch all exceptions thrown by a running program.

However, sometimes this can help to see all thrown exceptions. You can do this by attaching a debugger to the JVM and setting an “exception checkpoint” for all subclasses of java.lang.Throwable . Alternatively, you can write an interceptor to log all exceptions that cross the API boundary (any reasonable container for dependency injection can help with this). Or you can use the Instrumentation API to add registration to classes as they are loaded.

+2
source

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


All Articles