如何catch捕捉线程中的异常信息

走着路睡觉
  • java
小于 1 分钟

 public static void main(String[] args) {
        Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread th, Throwable ex) {
                System.out.println("异常信息" + ex);
            }
        };
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("Sleeping ...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Interrupted.");
                }
                System.out.println("Throwing exception ...");
                throw new RuntimeException();
            }
        };
        t.setUncaughtExceptionHandler(h);
        t.start();
    }
上次编辑于:
贡献者: zhaojingbo
Loading...