aspose file tools
The moose likes Threads and Synchronization and the fly likes Threading and Exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Threading and Exceptions." Watch "Threading and Exceptions." New topic
Author

Threading and Exceptions.

Jonny Butamer
Greenhorn

Joined: Oct 27, 2004
Posts: 15
Hi there,

I recently came across some code and I am hope someone will tell if the logic behind it is correct.
The code is summarised as follows:


What happens if the method, creates an instance of myThread, returns result, and then myThread throws a RuntimeException? Will that RuntimeException be caught by the surrounding try/catch and then throw a new instance of MyException?

Thanks for replies
Jonny Butamer
Greenhorn

Joined: Oct 27, 2004
Posts: 15
Small change to the code. It should be.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24051
    
  13

The answer to your question is "no". The only exceptions that catch block will ever see would occur during construction of the Thread and/or calling start(), and I'm not aware of any that could be thrown.

If a RuntimeException is thrown on the new Thread, it propagates on that Thread, eventually terminating it. The catch block should be inside the run(), not outside the Thread.start().

If the main Thread wants to know about the RuntimeException, then the new Thread has to catch it and send a message to the main Thread, one way or another -- either using wait() and notify(), or (more likely in this case) posting an error status to some well-known location.


[Jess in Action][AskingGoodQuestions]
Jonny Butamer
Greenhorn

Joined: Oct 27, 2004
Posts: 15
Thanks Ernest. Managed to find out what would happen by testing.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Threading and Exceptions.
 
Similar Threads
Exception
Exceptions (long one) Your response requested.
When will JVM exit? (just one more time pls...)
extending from Exception
compilation error in try/catch/finally