> Why do uncaught exceptions in a JButton action listener not cause the
> same behaviour as uncaught exceptions in a console application?
[quoted text clipped - 9 lines]
> Launcher displays a dialog box that says "Fatal exception occurred.
> Program will exit."
Not on my box it doesn't. Wrap that in a class and run it and you get..
"Exception in thread "main" java.lang.NullPointerException"
> However, consider the following code:
You'll have to start making some sense before I can advise further.
First, answer these questions,
- are you familiar with try/catch?
- why are you throwing exceptions in either case?
- why an NPE?
- what do you actually want to achieve?
HTH

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Kyle Blaney - 22 Nov 2004 14:54 GMT
Andrew,
I am trying to understand why there is different behaviour for
uncaught unchecked exceptions between a console application and a
Swing application. The code that I posted is not what my real
applications look like. Rather, I posted the minimal code required to
explain the situation I want to understand.
Now to your questions...
I am familiar with try/catch and I know that I could put a try/catch
block in every one of my event handlers. However, why does the Swing
application behave differently than the console application? Do the
Swing classes ground uncaught exceptions somewhere?
I am throwing exceptions in the posted code to show the situation I
want to understand. In my real applications, the exceptions would
actually be thrown much deeper.
I am throwing a NullPointerException because it is an unchecked
exception. Any unchecked exception would work just as well to help me
understand the situation.
Do I make sense yet?
Kyle
> > Why do uncaught exceptions in a JButton action listener not cause the
> > same behaviour as uncaught exceptions in a console application?
[quoted text clipped - 24 lines]
>
> HTH
> How do I get the same "Fatal exception occurred" dialog box to display
> when the Yes button is clicked?
Why do you want that your programm exits with such strange error message?

Signature
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
> Why do uncaught exceptions in a JButton action listener not cause the
> same behaviour as uncaught exceptions in a console application?
Because different code is handling the "uncaught" exceptions.
> For example, consider the following console application:
>
[quoted text clipped - 6 lines]
> Launcher displays a dialog box that says "Fatal exception occurred.
> Program will exit."
You do not say which Java launcher you use to run this code, which has an
effect on the result. But I am guessing that you are using JDK 1.4.x and
running the code using the "javaw" launcher under Windows, since that is
the only launcher that behaves that way, as far as I know. JDK 1.3 did
not behave that way, and the behavior has been removed in JDK 1.5.
> However, consider the following code:
>
[quoted text clipped - 7 lines]
> When this application is run and the user clicks on the Yes button, an
> uncaught exception is thrown but no dialog box is displayed.
This is a completely different environment than the previous case. In
that case, you have the Java launcher handling exceptions from the main
method that it calls from the "main" thread. However, in this case, you
have the EventDispatchThread object running in the "AWT-EventQueue"
thread, calling your event handler.
> How do I get the same "Fatal exception occurred" dialog box to display
> when the Yes button is clicked?
With Java 1.5, you can use Thread.setDefaultUncaughtExceptionHandler() to
specify a handler on a per-thread basis. To set the handler for the event
dispatch thread, use EventQueue.invokeLater() or
EventQueue.invokeAndWait() to run a Runnable object in the event dispatch
thread. It can then use Thread.currentThread() to get the event dispatch
Thread object and set the handler.
With Java 1.4, things get a little dicier, as there is no standard way to
do this. But there is a way that works with Sun JREs: you can set the
System property "sun.awt.exception.handler" to the name of an exception
handler class as follows:
System.setProperty( "sun.awt.exception.handler",
"com.acme.somepackage.ExceptionHandler" );
======================================================
package com.acme.somepackage;
public class ExceptionHandler {
public void handle( Throwable throwable ) {
// Handle the throwable:
JOptionPane.showMessageDialog( null, throwable );
}
}

Signature
Regards,
John McGrath