线程类静态void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler) (Thread Class static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler))
This method is available in package java.lang.Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler).
包java.lang.Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler)中提供了此方法。
This method is used to set the Default handler called if any of the thread terminates abnormally if any exception raises and when we didn’t write any code to handle the uncaught exception.
此方法用于设置Default处理程序,如果任何线程引发异常并在我们没有编写任何代码来处理未捕获的异常时异常终止,则调用Default处理程序。
This method is static so this method is accessible with the class name too.
此方法是静态的,因此也可以使用类名访问此方法。
The return type of this method is void so it does not return anything.
此方法的返回类型为void,因此它不返回任何内容。
This method takes one parameter (Thread.UncaughtExceptionHandler excep_handler) it is the object to use as a default handler to handle the uncaught exception and return null if no other default handler.
此方法有一个参数( Thread.UncaughtExceptionHandler excep_handler ),它是用作默认处理程序的对象,用于处理未捕获的异常,如果没有其他默认处理程序,则返回null。
This method is best suitable if we forgot to write a code of uncaught exceptions so it is the default handler call automatically if a thread terminates abruptly.
如果我们忘记编写未捕获的异常代码,则此方法最合适,因此,如果线程突然终止,它将自动成为默认的处理程序调用。
This method returns null if there is no other default handler.
如果没有其他默认处理程序,则此方法返回null。
Syntax:
句法:
static void setDefaultUncaughtExceptionHandler
(Thread.UncaughtExceptionHandler excep_handler){
}
Parameter(s):
参数:
We pass only one object as a parameter in the method that is (Thread.UncaughtExceptionHandler excep_handler), so it is the object to use as a default handler for uncaught exception else null if there is no default handler defined.
我们仅在方法( Thread.UncaughtExceptionHandler excep_handler )中传递一个对象作为参数,因此该对象用作未捕获异常的默认处理程序,如果未定义默认处理程序,则为null。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回类型为void ,它不返回任何内容。
Java程序演示setDefaultUncaughtExceptionHandler()方法的示例 (Java program to demonstrate example of setDefaultUncaughtExceptionHandler() 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 DefaultUncaughtExceptionHandlerClass extends Thread {
// Override run() of Thread class
public void run() {
throw new RuntimeException();
}
}
class Main {
public static void main(String[] args) {
// Creating an object of DefaultUncaughtExceptionHandlerClass class
DefaultUncaughtExceptionHandlerClass uehc =
new DefaultUncaughtExceptionHandlerClass();
// Creating an object of Thread class
Thread th = new Thread(uehc);
/* setDefaultUncaughtExceptionHandler
(Thread.UncaughtExceptionHandler excep_handler)
will set the handler for uncaught exception when
this thread terminate abnormally
*/
th.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread th, Throwable ex) {
System.out.println(th + " throws exception " + ex);
}
});
th.start();
}
}
Output
输出量
E:\Programs>javac Main.java
E:\Programs>java Main
Thread[Thread-1,5,main] throws exception java.lang.RuntimeException