Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Where does a RuntimeException in actionPerformed() go?  RSS feed

 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've run into something I've not expected. I have a very simple GUI application which does not need to be very robust. So in my main() method I catch a Throwable, and if there are any problems I simply throw a RuntimeException, the catch{} block displays a dialog box with the error message and the application exits.

However, when I throw a RuntimeException from the actionPerformed() method the exception never gets passed up the chain to my main() method! Now that I see this behavior I assume it's because actionPerformed() is being executed in a thread which main() is not.

So my question is, if I want to make some default error handling code for all of my actionPerformed() methods, where do I put that code?
 
Rodion Alukhanov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution looks like this:


It works. Unfortunately I could not find any official information from Sun to this code.
[ December 02, 2008: Message edited by: Rodion Alukhanov ]
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 1.5 or up you can use Thread.UncaughtExceptionHandler. You can either set it per thread, or for all threads at once using the static method.
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think so Rob. The reason is if you see the code of pumpOneEventForHierarchy, you will see that it will handle all exceptions, errors(and ThreadDeath...jeez). So even if you add a UncaughtExceptionHandler, its useless. The idea is that he should wrap his potential NPE throwing code in a try-catch within the actionPerformed method itself.

cheers,
suraj
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suraj Chandran:
I dont think so Rob. The reason is if you see the code of pumpOneEventForHierarchy, you will see that it will handle all exceptions, errors(and ThreadDeath...jeez). So even if you add a UncaughtExceptionHandler, its useless. The idea is that he should wrap his potential NPE throwing code in a try-catch within the actionPerformed method itself.

cheers,
suraj

Try the following:

When I click, it shows the message dialog, and the first row shows it is using the Event Dispatcher Thread.
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah! It does work!!!

cheers,
suraj
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!