Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make runnable Jar programs that do not fail silently?

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 hereenter 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?

like image 872
Pacerier Avatar asked Nov 11 '22 04:11

Pacerier


1 Answers

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..");
like image 124
user3505725 Avatar answered Nov 14 '22 23:11

user3505725