This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes uncaught exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "uncaught exception handling" Watch "uncaught exception handling" New topic
Author

uncaught exception handling

Nicole Lacoste
Ranch Hand

Joined: Oct 04, 2006
Posts: 30
Hi,

Kind of a general question. Here's my situation...

I have an application that starts lots of threads that are dependent on each other (producers and consumers). If one of the threads die I want the other threads to know about it. All my threads respond to interrupts and all my threads are subclasses of a class, MyThread, that has a method which interrupts all the other threads in the same group if the thread dies from a non null throwable. Anywhere in the run of my Threads where I return, I call dying with either an exception or null.

The problem is with uncaught exceptions. There seems to be two ways to solve the problem;
1. override the uncaughtException method as


2. wrap all the code in my runs with a



What are the reasons to choose one over the other?

Thanks,

Nicole

PS one thing I noticed is that for ordinary try-catch-finally blocks the finally is called after the catch, but if I use the uncaughtException handler the order is try-finally-uncaughtException.
[ February 20, 2007: Message edited by: Nicole Lacoste ]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I think either works. Are you using threadGroups here?
Nicole Lacoste
Ranch Hand

Joined: Oct 04, 2006
Posts: 30
Hi,


Yes I am using ThreadGroups (sorry if that wasn't clear), and yes both approached work. I was just wondering if there is some reason to prefer using thread groups and uncaught exceptions over try catch in the run. There might be something I just haven't thought of.

Nicole
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I would prefer method 1 since I think its a bit more clear what you are doing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uncaught exception handling
 
Similar Threads
When does the JVM exit?
throw an exception from UncaughtExceptionHandler
Why use UncaughtExceptionHandler?
Exception handling
Java Filter and uncaughtExceptions