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.\"
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)