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.
-
Thought about instrumentation? You can inject code that will be executed each time a new exception is initialized. – danieln Apr 09 '13 at 10:34
-
Does that require additional libraries because i am hoping to use no additional libraries i am not experianced with doing hacks like that – Josh Sobel Apr 09 '13 at 10:36
-
Yes it will require additional libraries. – danieln Apr 09 '13 at 10:39
3 Answers
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.
This can be done using Spring AOP which is useful in these cases.
You will require Spring AOP libraries for that.
A similar question was answered previously.

- 1
- 1

- 41,187
- 18
- 82
- 120
-
-
no spring aop is not just for J2EE applications can also be used with Core applications. And you get the advantage of cleaner code. – Narendra Pathai Apr 09 '13 at 13:42
This can be done through Java Debug Interface (JDI) that is part of Java Platform Debugger Architecture (JPDA).
In particular, see ExceptionRequest
and ExceptionEvent
.

- 486,780
- 108
- 951
- 1,012