1 package com.nilhcem.fakesmtp.core.exception;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6 import javax.swing.JOptionPane;
7 import javax.swing.SwingUtilities;
8 import java.awt.Component;
9
10
11
12
13
14
15
16
17 public final class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
18
19 private Component parentComponent;
20 private static final Logger LOGGER = LoggerFactory.getLogger(UncaughtExceptionHandler.class);
21
22
23
24
25
26
27
28 @Override
29 public void uncaughtException(final Thread t, final Throwable e) {
30 try {
31 if (SwingUtilities.isEventDispatchThread()) {
32 showException(t, e);
33 } else {
34 SwingUtilities.invokeLater(new Runnable() {
35 public void run() {
36 showException(t, e);
37 }
38 });
39 }
40 } catch (Exception excpt) {
41 LOGGER.error("", excpt);
42 }
43 }
44
45
46
47
48
49
50 public void setParentComponent(Component parentComponent) {
51 this.parentComponent = parentComponent;
52 }
53
54
55
56
57
58
59
60 private void showException(Thread t, Throwable e) {
61 LOGGER.error("", e);
62 String msg = String.format("Unexpected problem on thread %s: %s", t.getName(), e.getMessage());
63 JOptionPane.showMessageDialog(parentComponent, msg);
64 }
65 }