views:

496

answers:

3

Hello,

Is there a way to make a global exception-handler in Java. I want to use like this:

"When an exception is thrown somewhere in the WHOLE program, exit."

The handler may not catch exceptions thrown in a try-catch body.

Martijn

+7  A: 

Use Thread.setDefaultUncaughtExceptionHandler. See Rod Hilton's "Global Exception Handling" blog post for an example.

bobbymcr
+2  A: 

You can set the default UncaughtExceptionHandler , which will be used whenever a exception propegates uncaught throughout the system.

Kolibri
A: 

DefaultUncaughtExceptionHandler is the correct answer. It was revealed to me by Jeff Storey at this location, a few days ago. As u suspected, the "manually" caught exceptions will never be caught by this handler. However i got the following warning :

**- To be compliant to J2EE, a webapp should not use any thread.**

when i have checked my project against good-practice and recommended java coding style with PMD plug-in for Eclipse IDE.

Hypercube
"**- To be compliant to J2EE, a webapp should not use any thread.**" - I'm not aware of this. Do you mean that the service() (and doGet(), doPost(), etc.) method in a servlet should not spawn new threads?
Kolibri
I have no idea what that means, as my app is not web based. I've google it for a while, and i haven't found any answers, i'm affraid. Besides warnings like this or "variable name is too short" or "variable name is too long", i have founded PMD to be a great source of inspiration. I strongly recommend it.
Hypercube
I did a bit of digging around, and apparently you cannot directly create threads in J2EE. I've found a couple of links: http://www.theserverside.com/discussions/thread.tss?thread_id=44353 and http://stackoverflow.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged
Kolibri
Roger that, interesting..
Hypercube