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.

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.