Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2004

Tip: Looking for answers? Try searching our database.

Uncaught exceptions in JButton action listener

Thread view: 
Kyle Blaney - 22 Nov 2004 03:13 GMT
Why do uncaught exceptions in a JButton action listener not cause the
same behaviour as uncaught exceptions in a console application?

For example, consider the following console application:

public static void main( String[] args )
{
  throw new NullPointerException();
}

When this console application is run, the Java Virtual Machine
Launcher displays a dialog box that says "Fatal exception occurred.
Program will exit."

However, consider the following code:

JButton button = new JButton( "Yes" );
button.addActionListener( new ActionListener() {
  public void actionPerformed( ActionEvent e ) {
     throw new NullPointerException(); }
});
JOptionPane.showMessageDialog( null, button );

When this application is run and the user clicks on the Yes button, an
uncaught exception is thrown but no dialog box is displayed.

How do I get the same "Fatal exception occurred" dialog box to display
when the Yes button is clicked?
Andrew Thompson - 22 Nov 2004 06:07 GMT
> 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
Andrei Kouznetsov - 22 Nov 2004 07:16 GMT
> 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

John McGrath - 22 Nov 2004 08:15 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.