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