This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Swing and RuntimeException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Swing and RuntimeException" Watch "Swing and RuntimeException" New topic
Author

Swing and RuntimeException

Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
Sorry, but I fail to find any topic on this basic concept. Hope someone can point me in the right direction. Here is my problem:


I know the swing event are running in a special thread.

In the default mode, any RuntimeException which occurs in the swing thread is display in the console but the program continue to catch other swing events and run as if nothing important as occurs.


I have 2 questions:

1. How to set the program in a way that any swing RuntimeException stop the whole application?

2. How can I set an action before the stop of the whole application generate by the first method? (like a popup alert containing the error message and any release of lock, etc.)

Here is a little program as possible starting point for the discussion, keep in mind that we will make it as if it was a far bigger hierarchical gui application and that I am not aware of the fact that a division by 0 can lead to a RuntimeException.

(I know I can try / catch the exception in actionPerformed ... but I want that any RuntimeException, that I am not currently aware of or that I think should not happen, stop my application after having display a popup, log the error and/or do any other kind of action)



Thanks in advance for your help !
[ October 18, 2006: Message edited by: Eric Janssens ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961

1. How to set the program in a way that any swing RuntimeException stop the whole application?


I guess I must start saying that java.lang.RuntimeException is used for programming errors. Therefore, instead of trying to handle this kind of exception, you must fix the code for this kind of exceptions never happen.

For instance, an ArithmeticException happens when you try to perform a division by zero. You must not handle this exception, because this is never expected to happen, you must correct this kind of bugs.

Now, that said, what you want to do can be carried out in two different ways, with slight differences.

The first procedure is barely documented strategy. You start by defining a class like this:



Then, before the Event Dispatch Thread (The Swing main thread) is started you initialize a system property like this:



Make sure that the name of the class ErrorHandler is the fully quallified name of the class (i.e. com.javaranch.ErrorHandler), not just its cannonical name (ErrorHandler).

And voila! Magically this method will be called every time an uncaught exception happens in Swing (the Event Dispatch Thread).

You see how this mechanism works reading the source code in the java.awt.EventDispatchThread which is not public class and that is why you will not see its documentation in the javadoc API.

The second mechanism you can use is only available in JDK 1.5. since the new API incorporates a new method: Thread.setUncaughtExceptionHandler().

All you have to do is to implement Thread.UncaughtExceptionHandler and assign an instance of this implementation to the Event Dispatch Thread.

For example (this code must run within the EventDispathThread):




2. How can I set an action before the stop of the whole application generate by the first method? (like a popup alert containing the error message and any release of lock, etc.)


Evidently, in either approach you could display a dialog box within the event handler. The only problem is that by doing this you could be creating a new EventDispatchThread. The first approach will continue to handle exception, in the second approach you would have to register again the UncaughtExceptionHandler.

Another approach for this could be to use the Runtime.addShutdownHook(Thread t) which would give you the chance to execute some code before the exit() has had a chance to finish the JVM.

All this seems to me like too much work just to handle an exception that should have never happened in the first place.
[ October 18, 2006: Message edited by: Edwin Dalorzo ]
Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
Thank you very much !!!

As you have notice, my example is not very relevant but the main idea behind is "How to handle unexpected RuntimeException in a Swing application?".

unexpected = should never happen (programmation error OR unexpected system error)


You have perfectly answer to my question. Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing and RuntimeException