01: /*
02: * Copyright (c) 2005 Timothy Wall, All Rights Reserved
03: */
04: package abbot.util;
05:
06: import abbot.*;
07:
08: /** Provide access to the most recent exception caught on the event
09: dispatch thread.
10: */
11: public class EDTExceptionCatcher extends EventDispatchExceptionHandler {
12:
13: private static Throwable throwable = null;
14: private static long when = -1;
15:
16: public void install() {
17: clear();
18: super .install();
19: }
20:
21: /** Return the most recent exception caught on the dispatch thread, or
22: <code>null</code> if none has been thrown or the exception has been
23: cleared. Also clears the exception.
24: */
25: public synchronized static Throwable getThrowable() {
26: Throwable t = throwable;
27: clear();
28: return t;
29: }
30:
31: /** Returns when the most recent exception was caught on the dispatch
32: thread, or -1 if none has been thrown or the exception has been
33: cleared.
34: */
35: public synchronized static long getThrowableTime() {
36: return when;
37: }
38:
39: public synchronized static void clear() {
40: throwable = null;
41: when = -1;
42: }
43:
44: protected void exceptionCaught(Throwable thr) {
45: if (!(thr instanceof ExitException)) {
46: if (System.getProperty("java.class.path")
47: .indexOf("eclipse") != -1) {
48: Log.warn("An exception was thrown on the EDT: " + thr,
49: thr);
50: } else {
51: Log.log("An exception was thrown on the EDT: " + thr,
52: thr);
53: }
54: synchronized (EDTExceptionCatcher.class) {
55: when = System.currentTimeMillis();
56: throwable = thr;
57: }
58: }
59: }
60: }
|