5

Is there a way to get notified about every single exception that is thrown during runtime (handled and unhandled)?

What I want is a logger mechanism that logs every exception that comes up while my program is running. I don't want to handle the exceptions with this logger, I just want to be able to log the event of an exception being thrown.
The thing is that I want to include all system exceptions as well therefore it is not possible to call a function whenever I throw a new exception...

I have read something about ExceptionListener but they seem to be intended for a different job.

Has anyone an idea how this would be possible?

Raven
  • 2,951
  • 2
  • 26
  • 42
  • This answer might do what you want: http://stackoverflow.com/a/20284294/611819 – dnault Dec 23 '15 at 22:49
  • looks promising...but this would only allow me to get the exceptions handled in a try/catch-block... – Raven Dec 23 '15 at 23:07
  • Why do you want to do this? If an exception occurs and is handled as part of normal processing, why should anyone care. If you have some code that catches ALL exceptions and throws them away... You should fix that code to behave better. – Raedwald Dec 23 '15 at 23:37
  • Whenever an exception occurs it means that something went wrong in my program. If my code can handle it that's cool but I still want to know about that because eventually I can prevent the error from occuring completely – Raven Dec 23 '15 at 23:43
  • 1
    No, an exception does not always indicate that something went wrong. – Raedwald Dec 24 '15 at 09:18
  • What would be an example of an exception that didn't occure because something went wrong? As far as I know the concept behind exceptions is errorHandling... – Raven Dec 24 '15 at 11:57
  • For example, you have a server, and a client disconnects while you are writing to the client. There is no error in the server. – Raedwald Mar 24 '16 at 08:01

1 Answers1

2

Exceptions are usually logged when caught (rather than thrown), so their consequences can be logged, too. That is, some exceptions have no consequences, therefore warrant no investigation, while other exceptions are extremely critical, and must be investigated whenever they occur. A good logfile should therefore convey not only that an exception occurred, but also the (business) impact of that exception, which is known only after the exception is caught. This requires that exceptions be logged when they are handled.

Therefore, there is no Java API to intercept all exceptions thrown by the running program.

However, it can sometimes help to look at all thrown exceptions. You can do so by attaching a debugger to the JVM, and set an "exception breakpoint" for all subclasses of java.lang.Throwable. Alternatively, you can write an interceptor to log all exceptions that cross some API boundary (any reasonable dependency injection container can help with that). Or you might use the Instrumentation API to add logging to classes as they are loaded.

meriton
  • 68,356
  • 14
  • 108
  • 175