2

I am writing a game program, and rarely i will get an exception. I want to be able to record all the exceptions i get on a separate thread. My program is already multi-threaded. In some cases i use try catch, and for those i could just set an exception variable to the caught exception. However i want to be able to find out all exceptions that have been thrown on all threads without putting every statement in a try catch. For clarification i want the exception object not just the name of the exception.

Josh Sobel
  • 1,268
  • 3
  • 14
  • 27

3 Answers3

2

You want the Thread.UncaughtExceptionHandler:

Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception. When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments. If a thread has not had its UncaughtExceptionHandler explicitly set, then its ThreadGroup object acts as its UncaughtExceptionHandler. If the ThreadGroup object has no special requirements for dealing with the exception, it can forward the invocation to the default uncaught exception handler.

See here for details.

Community
  • 1
  • 1
linski
  • 5,046
  • 3
  • 22
  • 35
0

This can be done using Spring AOP which is useful in these cases.

@AfterThrowing aspect

You will require Spring AOP libraries for that.

A similar question was answered previously.

Community
  • 1
  • 1
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

This can be done through Java Debug Interface (JDI) that is part of Java Platform Debugger Architecture (JPDA).

In particular, see ExceptionRequest and ExceptionEvent.

NPE
  • 486,780
  • 108
  • 951
  • 1,012