温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UncaughtExceptionHandler的示例分析

发布时间:2021-12-27 10:46:58 阅读:156 作者:小新 栏目:大数据
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍了UncaughtExceptionHandler的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

        Thread的run方法是不抛出任何检查型异常(checked exception)的,但是它自身却可能因为一个异常而被终止,导致这个线程的终结。最麻烦的是,在线程中抛出的异常即使使用try...catch也无法截获,因此可能导致一些问题出现,比如异常的时候无法回收一些系统资源,或者没有关闭当前的连接等等。 

        JDK5.0之前,不能为单独的Thread设置UncaughtExceptionHandler,也不能指定一个默认的UncaughtExceptionHandler。为了可以设置一个UncaughtExceptionHandler,需要去继承ThreadGroup并覆写uncaughtException方法。 

        在JDK5.0中,我们通过Thread的实例方法setUncaughtExceptionHandler,可以为任何一个Thread设置一个UncaughtExceptionHandler。当然你也可以为所有Thread设置一个默认的UncaughtExceptionHandler,通过调用Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)方法,这是Thread的一个static方法。 

        定义一个Handler类必须实现Thread.UncaughtExceptionHandler接口的void uncaughtException(Thread t, Throwable e)方法。如果不设置一个Handler,那么单个Thread的Handler是null。但是,如果这个单个线程是ThreadGroup中的一个Thread,那么这个线程将使用ThreadGroup的UncaughtExceptionHandler。ThreadGroup自身已经实现了Thread.UncaughtExceptionHandler接口。 

        uncaughtException(Thread a, Throwable e)可以拿到Thread,所以在uncaughtException释放相关资源是最好的办法。

        总之,JDK5.0中Thread及其相关的辅助功能得到了加强,为我们提供了很多便利和安全的解决方案.

以下是UncaughtExceptionHandler的示例,大家可以运行一下看看

public class UncaughtExceptionHandlerSample {    public static void main(String[] args) {        //注释和没有注释的区别        Thread.setDefaultUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler());        UncaughtThread a = new UncaughtThread();        a.start();    }}/** * 自定义的一个UncaughtExceptionHandler */class DefaultUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {    /**     * 这里可以做任何针对异常的处理,比如记录日志等等     */    public void uncaughtException(Thread thread, Throwable e) {        if (e != null && e instanceof Error) {            if (e instanceof OutOfMemoryError) {                try {                    System.err.println("Halting due to Out Of Memory Error..." + Thread.currentThread().getName());                } catch (Throwable err) {                    // Again we don't want to exit because of logging issues.                }                Runtime.getRuntime().halt(-1);            } else {                // Running in daemon mode, we would pass Error to calling thread.            }        } else {            System.out.println("An exception has been capturedn");            System.out.println("Thread: " + thread.getId());            System.out.println("Exception: " + e.getClass().getName() + ":" + e.getMessage());            System.out.println("Thread status: " + thread.getState());            System.out.printf("Stack Trace: n");            e.printStackTrace(System.out);            new Thread(new UncaughtThread()).start();        }    }}/** * Test线程 */class UncaughtThread extends Thread {    public void run() {        System.out.println(Integer.parseInt("1"));        System.out.println(Integer.parseInt("2"));        System.out.println(Integer.parseInt("3"));        System.out.println(Integer.parseInt("abc"));    }}

感谢你能够认真阅读完这篇文章,希望小编分享的“UncaughtExceptionHandler的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/4587475/blog/4602864

AI

开发者交流群×