1 package net.sf.openrocket.gui.main;
3 import javax.swing.JOptionPane;
4 import javax.swing.SwingUtilities;
6 import net.sf.openrocket.gui.dialogs.BugReportDialog;
9 public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
11 private static final int MEMORY_RESERVE = 512*1024;
14 * A memory reserve of 0.5 MB of memory, that can be freed when showing the dialog.
16 private static volatile byte[] memoryReserve = null;
18 private static ExceptionHandler instance = null;
21 private volatile boolean handling = false;
27 public void uncaughtException(final Thread t, final Throwable e) {
29 // Free memory reserve if out of memory
30 if (e instanceof OutOfMemoryError) {
38 System.err.println("Exception is currently being handled, " +
39 "dumping exception instead:");
47 if (SwingUtilities.isEventDispatchThread()) {
50 SwingUtilities.invokeAndWait(new Runnable() {
57 } catch (Throwable ex) {
59 // Make sure the handler does not throw any exceptions
61 System.err.println("Exception in exception handler, dumping exception:");
63 } catch (Throwable ignore) { }
66 // Mark handling as completed
74 * The actual handling routine.
76 * @param t the thread that caused the exception, or <code>null</code>.
77 * @param e the exception.
79 private void showDialog(Thread t, Throwable e) {
82 if (e instanceof OutOfMemoryError) {
83 JOptionPane.showMessageDialog(null,
86 "<html>You should immediately close unnecessary design windows,<br>" +
87 "save any unsaved designs and restart OpenRocket!"
88 }, "Out of memory", JOptionPane.ERROR_MESSAGE);
93 if (!(e instanceof Exception)) {
94 JOptionPane.showMessageDialog(null,
96 "An unknown Java error occurred:",
98 "<html>You should immediately close unnecessary design windows,<br>" +
99 "save any unsaved designs and restart OpenRocket!"
100 }, "Unknown Java error", JOptionPane.ERROR_MESSAGE);
105 // Normal exception, show question dialog
107 int selection = JOptionPane.showOptionDialog(null, new Object[] {
108 "OpenRocket encountered an uncaught exception. This typically signifies " +
109 "a bug in the software.", " ",
110 "Please take a moment to report this bug to the developers.",
111 "This can be done automatically if you have an Internet connection."
112 }, "Uncaught exception", JOptionPane.DEFAULT_OPTION,
113 JOptionPane.ERROR_MESSAGE, null,
114 new Object[] { "View bug report", "Close" }, "View bug report");
116 if (selection != 0) {
121 // Show bug report dialog
122 BugReportDialog.showExceptionDialog(null, t, e);
129 * Registers the uncaught exception handler. This should be used to ensure that
130 * all necessary registrations are performed.
132 public static void registerExceptionHandler() {
134 if (instance == null) {
135 instance = new ExceptionHandler();
136 Thread.setDefaultUncaughtExceptionHandler(instance);
138 // Handler for modal dialogs of Sun's Java implementation
139 // See bug ID 4499199.
140 System.setProperty("sun.awt.exception.handler", AwtHandler.class.getName());
148 private static void reserveMemory() {
149 memoryReserve = new byte[MEMORY_RESERVE];
150 for (int i=0; i<MEMORY_RESERVE; i++) {
151 memoryReserve[i] = (byte)i;
157 * Handler used in modal dialogs by Sun Java implementation.
159 public static class AwtHandler {
160 public void handle(Throwable t) {
161 if (instance != null) {
162 instance.uncaughtException(Thread.currentThread(), t);