5

For the runnable Jar programs I create, I can read the unhandled exceptions if I run it from the command prompt (java -jar myprogram.jar).

Example code:

public static void main(String args[]) {
    try {
        EntireProgram();
    } catch (Throwable t) {
        throw new Error("somehow our error handling code has errors");
    }
}

public static void EntireProgram() {
    System.out.println(1 / 0);
}

Command line output:

enter image description here

However, if I run the Jar program by double-clicking it (expected of most end-users), the unhandled exceptions stay silent. I've noticed that this is in contrast to "native Windows programs" which I believe have unhandled exceptions handled by the OS default handler (modal dialog message):

enter image description here

enter image description here enter image description here

The advantage of handling unhandled exceptions this way is that the user will know something wrong has happened (and perhaps report it, hopefully).

How can I make my program's unhandled exceptions be handled in such a way?

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634

1 Answers1

1

Why don't you add a single line like this in your catch block ..which will show you the error message irrespective of How you run your app.

javax.swing.JOptionPane.showMessageDialog(null,"Something is really wrong..Hence notifying you..");
user3505725
  • 265
  • 1
  • 2
  • 11
  • 1
    I don't think this will work. I got stuck in a problem, where my `swing` based application requires `Java1.6` and above, but when I was executing that application in lower version of Java it was not showing anything, even I had given `showMessageDialog` in catch block, as there was error called `ClassNotFoundError`. – Vishrant Jul 25 '14 at 09:19
  • @user3505725, There *will* still be errors. We cannot expect code to be error-free, thus we cannot expect error-handling code (the showMessageDialog) to be error-free. How do we shift the responsibility of the error-handling to the OS? – Pacerier Jul 25 '14 at 09:24