I have a Controller class and a Monitor worker thread.
The controller thread looks something like this
public class ControllerA {
public void ControllerA(){
try{
doWork();
}
catch(OhNoException e){
//catch exception
}
public void doWork() throws OhNoException{
new Thread(new Runnable(){
public void run(){
//Needs to monitor resources of ControllerA,
//if things go wrong, it needs to throw OhNoException for its parent
}
}).start();
//do work here
}
}
Is such setup feasible? How do I throw exception to the outside of the thread?
Couple ways you can do this. You can set a
UncaughtExceptionHandleron the thread or you can use anExecutorService.submit(Callable)and use the exception that you get from theFuture.get().The easiest way is to use the
ExecutorService:If you want to use the
UncaughtExceptionHandlerthen you can do something like: