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
Joined: Oct 27, 2004
Small change to the code. It should be.
author and iconoclast
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.