Java线程类Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()方法(带示例)

线程类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]


翻译自: https://www.includehelp.com/java/thread-class-thread-uncaughtexceptionhandler-getuncaughtexceptionhandler-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值