8

Is there a way to listen any exceptions in java?

My purpose is creating a library that listens & collects all rised errors in java. Are there any way to do this?

Eren
  • 632
  • 1
  • 8
  • 16
  • No, there isn't an exception listener. You hava to write your own code in everhy try-catch. – Sergi Dec 20 '13 at 10:36

2 Answers2

4

You can catch every uncaught exception via Thread.UncaughtExceptionHandler. If that's not sufficient I would perhaps suggest some AOP/bytecode-weaving solution to implement some watch around each created exception.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

I have done some thing similar using Java instrumentation API, Create java agent and Class transformer to catch hold of required Exception class and instrument the byte code as necessary

you can follow up here Using Instrumentation to record unhandled exception

Above links will give you idea of how/when to use ASM or Instrumentation

Community
  • 1
  • 1
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46