• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing and RuntimeException

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic