During coding I want to catch some Exceptions from Runnables.
The code I tried to catch an Exception with:
public void fooBar(){
//nullable value
final String foo = null;
try{
new Thread(new Runnable() {
@Override
public void run() {
if(foo == null){
//Only Exception that can be thrown from a Runnable
throw new RuntimeException("F.U.B.A.R.");
}
}
}).start();
}catch(Exception ex){
//In my opinion the way to catch this Exception
System.out.println(ex.getMessage());
}
}
What I Expect to see as an output is:
F.U.B.A.R.
The output I'm getting:
Exception in thread "Thread-0" java.lang.RuntimeException: F.U.B.A.R.
The reason I need this is to put this Exception into my own so I can work with it properly down the line.
Does anyone know how to get the expected output instead of my current?
Because constructing Runnable and executing .start() between try and catch - did not throw exception. Runtime exception raised later, when thread executing "run" method.