Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
描述 (Description)
java.lang.Thread.getUncaughtExceptionHandler()方法返回当此线程由于未捕获的异常而突然终止时调用的处理程序。
声明 (Declaration)
以下是java.lang.Thread.getUncaughtExceptionHandler()方法的声明
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
参数 (Parameters)
NA
返回值 (Return Value)
此方法不返回任何值。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.Thread.getUncaughtExceptionHandler()方法的用法。
package com.iowiki;
import java.lang.*;
public class ThreadDemo implements Runnable {
Thread t;
public ThreadDemo() {
t = new Thread(this);
// this will call run() function
t.start();
}
public void run() {
// prints thread name
System.out.println("Thread = " + t.getName());
/* returns the handler invoked when this thread abruptly
terminates due to an uncaught exception. */
Thread.UncaughtExceptionHandler handler =
t.getUncaughtExceptionHandler();
System.out.println(handler);
}
public static void main(String[] args) {
new ThreadDemo();
new ThreadDemo();
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Thread = Thread-0
java.lang.ThreadGroup[name=main,maxpri=10]
Thread = Thread-1
java.lang.ThreadGroup[name=main,maxpri=10]