Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threading and Exceptions.  RSS feed

 
Jonny Butamer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Small change to the code. It should be.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jonny Butamer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest. Managed to find out what would happen by testing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!