提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在特定线程上注册UncaughtExceptionHandler。
public class Main {/*来 自 时代Java公众号 - nowjava.com*/ public static void main(String[] args) { Thread.currentThread().setUncaughtExceptionHandler( (Thread t, Throwable e) -> { System.out.println("In this thread " + t.getName() + " an exception was thrown " + e); }); Thread someThread = new Thread(() -> { System.out.println(200 / 0); }); someThread.setName("Some Unlucky Thread"); someThread.start(); /** 来自 n o w j a v a . c o m - 时 代 Java**/ System.out.println("In the main thread " + (200 / 0)); } }