Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect when an Exception's been thrown globally in Java?

Tags:

java

exception

How can I detect when an Exception has been thrown anywhere in my application?

I'm try to auto-magically send myself an email whenever an exception is thrown anywhere in my Java Desktop Application. I figure this way I can be more proactive.

I know I could just explicitly log and notify myself whenever an exception occurs, but I'd have to do it everywhere and I might(more likely will) miss a couple.

Any suggestions?

like image 511
Allain Lalonde Avatar asked Sep 16 '08 18:09

Allain Lalonde


People also ask

How do you find out where an exception is thrown?

You can install your own terminate() function by using std::set_terminate() . You should be able to set a breakpoint on your terminate function in gdb. You may be able to generate a stack backtrace from your terminate() function and this backtrace may help in identifying the location of the exception.

What is an exception and when are they thrown?

The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

When an exception is thrown what happens?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

What happens when an error is thrown in Java?

The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.


2 Answers

You probobly don't want to mail on any exception. There are lots of code in the JDK that actaully depend on exceptions to work normally. What I presume you are more inerested in are uncaught exceptions. If you are catching the exceptions you should handle notifications there.

In a desktop app there are two places to worry about this, in the event-dispatch-thread (EDT) and outside of the EDT. Globaly you can register a class implementing java.util.Thread.UncaughtExceptionHandler and register it via java.util.Thread.setDefaultUncaughtExceptionHandler. This will get called if an exception winds down to the bottom of the stack and the thread hasn't had a handler set on the current thread instance on the thread or the ThreadGroup.

The EDT has a different hook for handling exceptions. A system property 'sun.awt.exception.handler' needs to be registerd with the Fully Qualified Class Name of a class with a zero argument constructor. This class needs an instance method handle(Throwable) that does your work. The return type doesn't matter, and since a new instance is created every time, don't count on keeping state.

So if you don't care what thread the exception occurred in a sample may look like this:

class ExceptionHandler implements Thread.UncaughtExceptionHandler {   public void uncaughtException(Thread t, Throwable e) {     handle(e);   }    public void handle(Throwable throwable) {     try {       // insert your e-mail code here     } catch (Throwable t) {       // don't let the exception get thrown out, will cause infinite looping!     }   }    public static void registerExceptionHandler() {     Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());     System.setProperty("sun.awt.exception.handler", ExceptionHandler.class.getName());   } } 

Add this class into some random package, and then call the registerExceptionHandler method and you should be ready to go.

like image 61
shemnon Avatar answered Sep 21 '22 02:09

shemnon


The new debugging hooks in Java 1.5 let you do this. It enables e.g. "break on any exception" in debuggers.

Here's the specific Javadoc you need.

like image 45
Jason Cohen Avatar answered Sep 23 '22 02:09

Jason Cohen