线程类Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() (Thread Class Thread.UncaughtExceptionHandler getUncaughtExceptionHandler())
This method is available in package java.lang.Thread.getUncaughtExceptionHandler().
软件包java.lang.Thread.getUncaughtExceptionHandler()中提供了此方法。
This method is used to return the handler called if any of the thread terminates abnormally due to uncaught exception if any exception raises.
如果任何线程由于未捕获的异常而异常终止(如果引发任何异常),则此方法用于返回调用的处理程序。
This method is not static so this method is not accessible with classname too.
此方法不是静态的,因此也无法使用类名访问此方法。
The return type of this method is Thread.UncaughtExceptionHandler it provides the handler called when the thread terminates abnormally due to an uncaught exception.
该方法的返回类型为Thread.UncaughtExceptionHandler,它提供当线程由于未捕获的异常而异常终止时调用的处理程序。
This method returns null if it does not raise any exception that means normal termination of the thread.
如果此方法未引发任何异常,则该方法将返回null,这意味着正常终止线程。
Syntax:
句法:
Thread.UncaughtExceptionHandler getUncaughtExceptionHandler(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the Thread.
我们不会在Thread方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is Thread.UncaughtExceptionHandler, it returns the handler for uncaught exception when terminating thread abruptly else null if there is no fault means normal termination.
此方法的返回类型为Thread.UncaughtExceptionHandler ,当突然终止线程时,它将返回未捕获异常的处理程序;否则,如果没有错误意味着正常终止,则返回null。
Java程序演示getUncaughtExceptionHandler()方法的示例 (Java program to demonstrate example of getUncaughtExceptionHandler() method)
/* We will use Thread class methods so we are importing
the package but it is not mandate
because it is imported by default
*/
import java.lang.Thread;
class UncaughtExceptionHandlerClass extends Thread {
static Thread th1;
// Override run() of Thread class
public void run() {
//Display a message for the end user
System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
// getUncaughtExceptionHandler() will return the handler
// for uncaught exception when thread terminate abnormally
// and create a reference of Thread.UncaughtExceptionHandler
Thread.UncaughtExceptionHandler ueh = th1.getUncaughtExceptionHandler();
System.out.println("The handler for the thread is = " + ueh);
}
public static void main(String[] args) {
// Creating an object of UncaughtExceptionHandlerClass class
UncaughtExceptionHandlerClass uehc =
new UncaughtExceptionHandlerClass();
// Creating an object of Thread class
th1 = new Thread(uehc);
// Thread class start() method will call and it will ultimately
th1.start();
}
}
Output
输出量
E:\Programs>javac UncaughtExceptionHandlerClass.java
E:\Programs>java UncaughtExceptionHandlerClass
The name of this thread is Thread-1
The handler for the thread is = java.lang.ThreadGroup[name=main,maxpri=10]