Java: Handling exceptions in child threads

后端 未结 3 1495
庸人自扰
庸人自扰 2020-12-06 06:01

I prefer to have the exception handling logic further up in the call stack, near the main method. I like this approach... However, I created a thread where some of its metho

3条回答
  •  遥遥无期
    2020-12-06 06:49

    Catch it at the outer level of your run() method then place the Exception in a variable in your Runnable and have your Runnable indicate that it completed.

    The code that started your runnable has to then examine the Runnable to see that the "Exception" object is set and either rethrow it or deal with it.

    If you rethrow it, you may want to wrap it in a new exception:

    throw new Exception(oldException);
    

    This will give you both stack traces.

    (Thanks Taylor L)

提交回复
热议问题