Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Throwing Exception from Runnable

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a nested class instance of Runnable and am wanting to throw an Exception from the catch block in there.. It gives me compilation error. Is it not possible to throw an exception from there?
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"amit_94040",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by amit_94040:
Hi I have a nested class instance of Runnable and am wanting to throw an Exception from the catch block in there.. It gives me compilation error. Is it not possible to throw an exception from there?


no you can't because run() of runnable does'nt throw an exception.
You can't add exceptions while overriding / except when the exceptions are extending RuntimeException.

 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by amit_94040:
Hi I have a nested class instance of Runnable and am wanting to throw an Exception from the catch block in there.. It gives me compilation error. Is it not possible to throw an exception from there?


Where should the exception propagate to? Assuming you used the Runnable in a Thread constructor, that thread is essentially dead when an exception propagates out of the run() method. It can hardly propagate to the thread which created the Runnable. That thread has gone off on its own business and throwing exceptions at it out of nowhere isn't going to do a lot of good.
Presumably, some exception may be thrown in your Runnable and you are interested in preserving it to implement error handling. Some other Thread - maybe the thread that created it - will have to come back to the Runnable, check if everything went alright, and handle any errors that might have occurred.

The above code will allow this. It isn't particularly pretty but it does illustrate the idea.
A common twist used in multi-threaded services is for the exception to be wrapped inside another exception, which is then thrown. Take a look at SwingUtilities.invokeAndWait(). If your Runnable throws an exception, this method will throw an InvocationTargetException wrapper exception. This is only possible because the invokeAndWait() blocks until your Runnable has finished. Compare this to invokeLater(), which does not have a standard way of communicating exceptions back at you.
- Peter

[This message has been edited by Peter den Haan (edited June 14, 2001).]
 
amit_94040
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried putting my real Name in but it was taken. And I hate changing my firstname or my lastname and so I used this id which I'm used to..

Originally posted by Frank Carver:
"amit_94040",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.


 
We're all out of roofs. But we still have tiny ads:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic