0

Can an object that implements runnable throw an exception to its caller? I ask this cause the run method itself can't throw exceptions so i wonder how it could be achieved.

Basically the run method itself can throw an exception but others can't see it (RuntimeException) but can it throw an normal exception to the caller that invoked the thread?

Basically ive got a class that calls a thread to run but in that thread thats running if an event occours i want something to happen in the other class. However the thread doesn't know about the class that called it.

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Ezzaral
0

you can catch RuntimeExceptions if you want to. Though you should (in general) never catch RuntimeException, Exception, or Throwable because they're far too generic to handle correctly.

And no, you can NOT throw a checked exception from a method that isn't defined as not throwing a checked exception in its inheritance hierarchy.

0

You might want to look into whether you can use Callable for your task instead of Runnable. Callable throws Exception and returns a result. Using that, in conjunction with Future and an Executor perhaps, would be an option.

Since you seem to have a lot of questions on threading lately, you might want to pick up a copy of Java Concurrency in Practice. It covers a lot of ground on multi-threading and concurrency with the Java 5/6 concurrency packages.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.