Java:处理子线程中的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2631791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Handling exceptions in child threads
提问by Zombies
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 method calls inside run() may throw exceptions. I would really like to see if there is a way that these exceptions can be thrown back up to the parent thread? The best I could think of is setting a variable inside the object that implements Runnable. This variable is a string that contains the Error Message, which then uses a class loader to correctly re-create the same exception in the parent thread.
我更喜欢在调用堆栈中靠近 main 方法的地方有异常处理逻辑。我喜欢这种方法……但是,我创建了一个线程,其中 run() 中的某些方法调用可能会引发异常。我真的很想看看是否有办法将这些异常抛出回父线程?我能想到的最好的方法是在实现Runnable. 这个变量是一个包含错误消息的字符串,然后它使用类加载器在父线程中正确地重新创建相同的异常。
What I would like to know, is there a less messy way of getting what I want here? (to be able to make sure that any exception thrown in a child thread is handled with the same exception handling logic as though it was running in the main thread/code re-use).
我想知道的是,这里有没有一种不那么凌乱的方式来获得我想要的东西?(能够确保在子线程中抛出的任何异常都使用相同的异常处理逻辑进行处理,就好像它在主线程/代码重用中运行一样)。
采纳答案by Bill K
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.
在 run() 方法的外层捕获它,然后将异常放入 Runnable 中的变量中,并让 Runnable 指示它已完成。
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.
启动 runnable 的代码必须检查 Runnable 以查看是否设置了“异常”对象,然后重新抛出它或处理它。
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)
(感谢泰勒 L)
回答by John Vint
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()
您可以在此处使用 ExecutorService 提交可调用对象并接收 Future。在这一点上,您至少希望将异常传播到您将调用的调用线程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.
future.get() 会将 call 方法中抛出的任何异常传播到调用 future.get() 的线程。因此,如果您的主线程调用 future.get() 则主线程将看到抛出的任何异常。
回答by pajton
With Thread.setUncaughtExceptionHandler()you can set an Thread.UncaughtExceptionHandlerin your Threadand have a centralized logic of handling exceptions of your threads.
使用Thread.setUncaughtExceptionHandler()您可以在您的线程中设置一个Thread.UncaughtExceptionHandlerThread并拥有处理线程异常的集中逻辑。
Moreover you can write your own ThreadFactorythat will create you threads with preset Thread.UncaughtExceptionHandler.
此外,您可以编写自己的ThreadFactory来创建带有预设的线程Thread.UncaughtExceptionHandler。

