1

I want an app that throws an exception from a button.
The exception should then shutdown the application (unhandled exception).

( I need that to check my code in Runtime.getRuntime().addShutDownHook() )

So I wrote this

button1.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent e) {
        throw new NullPointerException("");
    }
});

And tried also tried throwing a RuntimeException, but the application did not close.

Any suggestion how can I close my app due to an exception?
Thanks.

EDIT
I will explain - In the Java docs - http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
it is noted that when you write a JVM shut down hook - make it a fast running piece of code. Quote:

"Shutdown hooks should also finish their work quickly...."

My code is running a bit longer and I wanted to test it by an exception (and not System.exit() - though it should be the same, but sometimes the results are not the same)

Just wondered how you throw an exception from a button (I know the code is bad, it's for testing ).

informatik01
  • 16,038
  • 10
  • 74
  • 104
Bick
  • 17,833
  • 52
  • 146
  • 251
  • 7
    You need to catch the exception and call `System.exit()` – Peter Lawrey Aug 21 '12 at 14:04
  • 1
    @PeterLawrey, `System.exit(1)` – user1329572 Aug 21 '12 at 14:05
  • 2
    This i not the right way to close an app. Look at this post http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code – Romain Hippeau Aug 21 '12 at 14:05
  • 1
    @PeterLawrey Make that an answer and you'll have an upvote. – David B Aug 21 '12 at 14:05
  • Why are you even throwing an exception? Can't you just call `System.exit()` when the button is clicked? – Chris Dargis Aug 21 '12 at 14:06
  • as said I wrote code and delegate it to the shut down hooks (using Runtime.getRuntime().addShutDownHook()) and I want to test it when it runs from an exception – Bick Aug 21 '12 at 14:06
  • I know it is not the right way. wait . I will edit. – Bick Aug 21 '12 at 14:06
  • @RomainHippeau I have looked at the post and it talks about how to have an application shutdown when a window is closed. I don't see how that helps here. – Peter Lawrey Aug 21 '12 at 14:07
  • What may be confusing you is that uncaught exceptions on the event dispatch thread do not bring down a Java application (as they would in a console application). If you wish to catch such exceptions, you should try the approach suggested here: http://ruben42.wordpress.com/2009/03/30/catching-all-runtime-exceptions-in-swing/ – Duncan Jones Aug 21 '12 at 14:09

3 Answers3

1

check this answer: How do I catch this exception in Swing?

In implementation of uncaughtException just exit your app.

Community
  • 1
  • 1
czajek
  • 714
  • 1
  • 9
  • 23
  • I'm not sure this works if the exception is on the event dispatch thread (see my comment in the original question). I think this approach only works in cases where the uncaught exception kills the thread in question, whereas the event dispatch thread is automatically restarted. – Duncan Jones Aug 21 '12 at 14:11
  • @DuncanJones I agree, see my answer. – Peter Lawrey Aug 21 '12 at 14:13
  • Sorry I don't get your point. If there is uncaught exception on EDT, EDT dies and new one is started. DefaultUncaughtException is fired in this case and everything works as described. regards – czajek Aug 21 '12 at 15:03
1

You need to catch the exception and call System.exit()

It is explained here in the comments (suggested by Duncan Jones ;)

The setDefaultUncaughtExceptionHandler will only work if the thread die (thread abruptly terminates due to an uncaught exception). But EventQueue thread will never die unless the application ends. Therefore without special handling all uncaught exception will be swallow by EventQueue and go into system out silently.

So I would add an uncaught exception handler to trap when other threads die, but to check exceptions throw in the EDT, you have to catch them yourself.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

any suggestion how can I close my app due to an exception?

For a start, throwing an exception from the action listener won't work. It will kill the Swing event dispatcher thread, but that won't make the application exit.

The simple solution is to call System.exit(...) from the listener to tell the JVM to shutdown. However, if that approach is a bit "brutal" and may cause tasks being performed asynchronous to be abandoned. You can ameliorate by using shutdown hooks to give the tasks to bail out, but it is still not nice. For instance, shutdown hooks don't fire in a predictable order ...

A better solution is to design your application to attempt to perform an orderly shutdown before it pulls the plug by calling System.exit(). For instance, once the "main" thread has launched the Swing dispatcher event thread, it could wait on a condition which tells it to shutdown. The action listener for the "exit" button would signal this condition, and the main thread would wake up and start shutting things down.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216