Reputation: 4149
How do I get the anonymous runnable
to make the parent method throw an exception?
public boolean drive2Exit() throws Exception {
while(true) {
handler.postDelayed(new Runnable() {
public void run() {
try {
...
catch (Exception e) {
//drive2Exit should throw exception
}
}
}, 100);
}
}
Upvotes: 1
Views: 320
Reputation: 2153
I think you would probably be best server with the use of a Future:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
using that you can in your main code check the result of the runnable and if it threw an exception you will get exception to process in the code checking the future. use that exception and i think the embedded original exception to decide what to throw outside the Future.
Upvotes: 1
Reputation: 5110
I don't think if you can do that. When exception get raised in child thread and it's not catched, child thread would simply die without parent knowing about it. What I can think of is that catch
the Exception
in child and put it in a shared data structure e.g. a static
variable shared among children and parent. Parent would periodically poll this data structure if there is any exception in it's child.
Or you can implement Observer
design pattern to notify main
thread when there is new exception in any of the child (i.e. shared data structure is changed.)
Upvotes: 0