线程抛出异常时会发生什么?

时间:2022-05-12 20:58:30

If I invoke the run() method on a Thread and the run() method throws an uncaught Exception what would be the outcome ?

如果我在Thread上调用run()方法并且run()方法抛出一个未被捕获的异常会产生什么结果呢?

Who catches this Exception? Does the Exception even get caught?

谁抓住了这个例外?异常甚至被抓住了吗?

3 个解决方案

#1


If there is an exception handler installed for the ThreadGroup, the JVM passes the exception to it. If it's an AWT thread, you can install an event handler for otherwise unhandled exceptions. Otherwise the JVM handles it.

如果为ThreadGroup安装了异常处理程序,则JVM会将异常传递给它。如果它是AWT线程,则可以为其他未处理的异常安装事件处理程序。否则JVM会处理它。

Example of a thread group with a custom handler and how to use it:

具有自定义处理程序的线程组示例以及如何使用它:

public class MyThreadGroup extends ThreadGroup {
    public MyThreadGroup() {
        super("My Thread Group");
    }
    public void uncaughtException(Thread t, Throwable ex) {
        // Handle exception
    }
}

Thread t = new Thread(new MyThreadGroup(), "My Thread") { ... };
t.start();

Example of using an AWT exception handler:

使用AWT异常处理程序的示例:

public class MyExceptionHandler {
    public void handle(Throwable ex) {
        // Handle exception
    }
    public void handle(Thread t, Throwable ex) {
        // Handle exception
    }
}

System.setProperty("sun.awt.exception.handler", MyExceptionHandler.class.getName());

#2


If you've submitted the Runnable to an ExecutorService you can catch the Exception as wrapped inside a ExecutionException. (Highly recommended over simply calling run())

如果您已将Runnable提交给ExecutorService,则可以捕获ExecutionException中包含的Exception。 (强烈推荐简单地调用run())

#3


It can if you assign it to a ThreadGroup that implements the uncaughtException(Thread, Throwable) method.

如果将它分配给实现uncaughtException(Thread,Throwable)方法的ThreadGroup,则可以。

#1


If there is an exception handler installed for the ThreadGroup, the JVM passes the exception to it. If it's an AWT thread, you can install an event handler for otherwise unhandled exceptions. Otherwise the JVM handles it.

如果为ThreadGroup安装了异常处理程序,则JVM会将异常传递给它。如果它是AWT线程,则可以为其他未处理的异常安装事件处理程序。否则JVM会处理它。

Example of a thread group with a custom handler and how to use it:

具有自定义处理程序的线程组示例以及如何使用它:

public class MyThreadGroup extends ThreadGroup {
    public MyThreadGroup() {
        super("My Thread Group");
    }
    public void uncaughtException(Thread t, Throwable ex) {
        // Handle exception
    }
}

Thread t = new Thread(new MyThreadGroup(), "My Thread") { ... };
t.start();

Example of using an AWT exception handler:

使用AWT异常处理程序的示例:

public class MyExceptionHandler {
    public void handle(Throwable ex) {
        // Handle exception
    }
    public void handle(Thread t, Throwable ex) {
        // Handle exception
    }
}

System.setProperty("sun.awt.exception.handler", MyExceptionHandler.class.getName());

#2


If you've submitted the Runnable to an ExecutorService you can catch the Exception as wrapped inside a ExecutionException. (Highly recommended over simply calling run())

如果您已将Runnable提交给ExecutorService,则可以捕获ExecutionException中包含的Exception。 (强烈推荐简单地调用run())

#3


It can if you assign it to a ThreadGroup that implements the uncaughtException(Thread, Throwable) method.

如果将它分配给实现uncaughtException(Thread,Throwable)方法的ThreadGroup,则可以。