Java线程类静态无效setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler)方法,带有示例...

本文详细介绍了Java线程类的静态方法setDefaultUncaughtExceptionHandler,该方法用于设置默认的未捕获异常处理器。当线程异常终止且未处理异常时,这个默认处理器会被调用。文章提供了一个示例程序来演示如何使用该方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程类静态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


翻译自: https://www.includehelp.com/java/thread-class-static-void-setdefaultuncaughtexceptionhandler-thread-uncaughtexceptionhandler-excep_handler-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值