java 如何捕获Java中另一个线程抛出的异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10351926/
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
How to catch exception thrown by another thread in Java?
提问by yuejdesigner85
I'm using a library that creates its own thread and it throws an exception. How can I catch that exception? The exception is thrown on the line marked below:
我正在使用一个创建自己线程的库,它会引发异常。我怎样才能捕捉到那个异常?在下面标记的行上抛出异常:
ResourceDescriptor rd = new ResourceDescriptor();
rd.setWsType(ResourceDescriptor.TYPE_FOLDER);
fullUri += "/" + token;
System.out.println(fullUri);
// >>> EXCEPTION THROWN ON THE FOLLOWING LINE <<<
rd.setUriString(fullUri.replaceAll("_", ""));
try{
rd = server.getWSClient().get(rd, null);
}catch(Exception e){
if(e.getMessage().contains("resource was not found")){
this.addFolder(fullUri, label, false);
System.out.println("Folder does not exist, will be added now.");
}else{
System.out.println("Error Messages: " + e.getMessage());
}
}
回答by Ortwin Angermeier
If you can not catch it maybe that helps you:
如果你不能抓住它,也许这对你有帮助:
If you have the Thread
object you can try to set a UncaughtExceptionHandler.
Take a look at Thread.setUncaughtExceptionHandler(...).
如果你有Thread
对象,你可以尝试设置一个UncaughtExceptionHandler。看看Thread.setUncaughtExceptionHandler(...)。
Give us some more detail about the library you use and how you use it.
向我们提供有关您使用的库以及如何使用它的更多详细信息。
回答by Gray
If all you have is a Thread
object then there is no way to catch any exceptions (which I assume are RuntimeException
). The proper way to do this is by using the Future<?>
class used by the ExecutorService
but you don't have control over the code starting the Thread
I assume.
如果您拥有的只是一个Thread
对象,则无法捕获任何异常(我认为是RuntimeException
)。正确的方法是使用 使用的Future<?>
类,ExecutorService
但您无法控制开始Thread
我假设的代码。
If you are providing the Runnable
or if you are injecting any of the code into the library, then you could wrap it in a class that catches and holds the Exception
for you but that is only if the exception is in your code or is thrown from within code you are calling. Something like the following:
如果您正在提供Runnable
或将任何代码注入库中,那么您可以将其包装在一个类中,该类可以Exception
为您捕获并保存该异常,但前提是异常在您的代码中或从代码中抛出你在打电话。类似于以下内容:
final AtomicReference<Exception> exception = new AtomicReference<Exception>();
Thread thread = library.someMethod(new Runnable() {
public void run() {
try {
// call a bunch of code that might throw
} catch (Exception e) {
// store our exception thrown by the inner thread
exception.set(e);
}
}
});
// we assume the library starts the thread
// wait for the thread to finish somehow, maybe call library.join()
thread.join();
if (exception.get() != null) {
throw exception.get();
}
Also, as @Ortwin mentions, if you are forking your own thread you can also set the uncaught exception handler:
此外,正如@Ortwin 所提到的,如果您要创建自己的线程,您还可以设置未捕获的异常处理程序:
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
// log it, dump it to the console, or ...
}
});
But if the thread code inside of the library cannot be wrapped by you then that won't work. If you edit your question and show some code and provide some more details, I can edit my question to provide better help.
但是,如果库中的线程代码不能由您包装,那么这将不起作用。如果您编辑问题并显示一些代码并提供更多详细信息,我可以编辑我的问题以提供更好的帮助。