Java: Handling exceptions in child threads

后端 未结 3 1498
庸人自扰
庸人自扰 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:56

    You can use an ExecutorService here to submit a callable and receive a Future. At the point you would at the very least want the Exception to be propagated to the invoking thread you would invoke future.get()

    future.get() will propogate any exception thrown in the call method to the thread that is invoking future.get(). So if your main thread invokes future.get() then the main thread will see any exceptions thrown.

提交回复
热议问题