Uncaught Exceptions ----《Pro_Java_8_Programming_(3rd_edition)》
2016-01-24 11:13
323 查看
Uncaught Exceptions
As mentioned earlier, a thread dies when it exits the run() method of the Runnable object with which it’sassociated. In most cases, this will occur when the thread has executed all the code within that method,
but it can also occur if an exception is thrown that’s not caught. For example, NullPointerException is
perhaps the most common exception that’s encountered by Java programmers, and it isn’t typically caught
and handled because there’s usually no way for the application to recover when a NullPointerException
is thrown. Assuming that a NullPointerException is thrown during execution of the run() method, either
within that method itself or within other code it calls, and assuming that no attempt is made to catch the
exception, it will cause the thread to die.
By default, an uncaught exception simply causes the thread’s stack trace to be printed before the thread
dies, but you can override this behavior using an uncaught exception handler. How you handle uncaught
exceptions depends upon whether you want to customize the behavior for all threads in a ThreadGroup or
you only want to change the behavior for a single thread. When an uncaught exception occurs for a thread
its getUncaughtExceptionHandler() method is called to determine if it has been assigned an instance of
the UncaughtExceptionHandler interface. If so, that object's uncaughtException() method is called and is
passed a reference to the thread and to the exception that occurred. If, on the other hand, no handler has
been assigned to the thread the uncaughtException() method is called for the ThreadGroup associated with
the thread and, as mentioned before, the behavior defined there is to simply display the stack trace of the
thread for which the exception occurred.
相关文章推荐
- java 中的printStackTrace()方法
- Spring的反射机制和依赖注入
- 关于WIN10系统登录银河超算出现java被安全设置阻止解决方法
- XML——解析XML文档
- java日期处理总结
- java锁(2)ReentrantReadWriteLock
- 2016蓝桥杯假期任务之《奇怪的分式》
- java锁(1)之ReentrantLock
- eclipse环境搭建之四:Go。但没有成功,后转为LiteIDE。
- struts2拦截器的实现原理及源码剖析
- 2016蓝桥杯假期任务之《圆周率》
- XML——XML概述
- Struts2自定义返回Json类型result
- spring2.5+JDK1.8报错的解决办法
- Spring学习笔记(3)之Spring对JDBC的支持
- Spring学习笔记
- Spring学习笔记(2)之Spring AOP
- Struts2学习笔记
- Sprint学习笔记(1)之Spring中的Bean配置
- JSTL---Servlet.service() for servlet action threw exceptionjavax.el.MethodNotFoundException Method s