Skip to content

Commit 48475f7

Browse files
committed
registering custom uncaught exception handler
1 parent 06ebdbd commit 48475f7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package de.doubleslash.keeptime;
2+
3+
import java.lang.Thread.UncaughtExceptionHandler;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
/**
9+
* Handler logging uncaught exceptions. Gotta Catch 'Em All
10+
*
11+
* @author nmutter
12+
*/
13+
public class DefaultExceptionHandler implements UncaughtExceptionHandler {
14+
15+
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
16+
17+
@Override
18+
public void uncaughtException(final Thread t, final Throwable e) {
19+
LOG.error("Uncaught exception on thread '{}'.", t, e);
20+
}
21+
22+
/**
23+
* Registers this class as default uncaught exception handler
24+
*/
25+
public void register() {
26+
LOG.debug("Registering uncaught exception handler");
27+
final UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
28+
if (defaultUncaughtExceptionHandler != null) {
29+
LOG.warn("Uncaught exception handler was already set ('{}'). Overwritting.", defaultUncaughtExceptionHandler);
30+
}
31+
Thread.setDefaultUncaughtExceptionHandler(this);
32+
}
33+
34+
}

src/main/java/de/doubleslash/keeptime/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class Main extends Application {
4848

4949
@Override
5050
public void init() throws Exception {
51+
final DefaultExceptionHandler defaultExceptionHandler = new DefaultExceptionHandler();
52+
defaultExceptionHandler.register();
53+
5154
springContext = SpringApplication.run(Main.class);
5255
// TODO test if everywhere is used the same model
5356
model = springContext.getBean(Model.class);

0 commit comments

Comments
 (0)