集册 Java实例教程 在特定线程上注册UncaughtExceptionHandler。

在特定线程上注册UncaughtExceptionHandler。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

668
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在特定线程上注册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));

  }

}