集册 Java实例教程 捕捉未捕获的异常

捕捉未捕获的异常

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

604
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
Java可以为每个线程或全局注册ExceptionHandler()。

import java.util.Random;


public class Main {/**from nowjava - 时代Java**/

  public static void main(String[] args) {

    Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {

      System.out.println("Woa! there was an exception thrown somewhere! "

          + t.getName() + ": " + e);

    });


    final Random random = new Random();

    for (int j = 0; j < 10; j++) {

      int divisor = random.nextInt(4);

      
展开阅读全文