42

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

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

6 Answers6

62

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

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
4

Here's an example which uses Logback to handle any uncaught exceptions:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
        LoggerFactory.getLogger("CustomLogger").error("Uncaught Exception in thread '" + t.getName() + "'", e);
        System.exit(1);
    }
});

This can also be done on a per-thread basis using Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
4

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

Kolibri
  • 567
  • 2
  • 6
2

For clarification, use setDefaultUncaughtExceptionHandler for standalone Java applications or for instances where you are sure you have a well-defined entry point for the Thread.

For instances where you do not have a well-defined entry point for the Thread, for example, when you are running in a web server or app server context or other framework where the setup and teardown are handled outside of your code, look to see how that framework handles global exceptions. Typically, these frameworks have their own established global exception handlers that you become a participant in, rather than define.

For a more elaborate discussion, please see http://metatations.com/2011/11/20/global-exception-handling-in-java/

1

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.

Community
  • 1
  • 1
hypercube
  • 958
  • 2
  • 16
  • 34
  • "**- 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 Oct 10 '09 at 18:06
  • 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 Oct 10 '09 at 18:14
  • 2
    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 Oct 10 '09 at 20:05
  • Thank you, @Kolibri. – hypercube Apr 11 '20 at 15:23
  • 1
    No serious web application runs only on one thread. "_a webapp should not use any thread_" means you shouldn't create new threads yourself, because the application has no control over those. It's perfectly fine to use preconfigured thread pools. – Abhijit Sarkar Sep 18 '20 at 05:31
0

Threads.setDefaultUncaughtExceptionHandler() works but not in all cases. For example, I'm using it in my main() before creating Swing widgets, and it works in the threads created by Swing, such as the AWT event thread or SwingWorker threads.

Sadly, it doesn't have any effect on the thread created by javax.naming.spi.NamingManager.getInitialContext() when using an LDAP URL, using JavaSE 1.6. No doubt there are other exceptions.

Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
  • Why don't you use `setUncaughtExceptionHandler` for newly created threads? – wonsuc Apr 19 '22 at 03:58
  • @wonsuc - I'm going to delete this answer, since it's not an answer. I'll add a comment above, instead. If your comment isn't addressed above you may want to add it somewhere. (My reason, I don't know 7 years later! But perhaps it was not easy to find the threads created by `NamingManagerGetInitialContext()`. Or perhaps I didn't know about it.) – Jeff Learman Apr 26 '22 at 14:49