How uncaught exceptions are handled
来源:互联网 发布:淘宝极有假货有假货吗 编辑:程序博客网 时间:2024/05/18 01:06
In our discussion of unchecked exceptions, we mentioned that when these exceptions are not caught in a try/catch block, then what you often see in practice is Java print the exception stack trace and then terminate your program. In reality, this is a simplistic view of what happens, and we'll look in more detail here.
Java actually handles uncaught exceptions according to the thread in which they occur. When an uncaught exception occurs in a particular thread, Java looks for what is called an uncaught exception handler, actually an implementaiton of the interface UncaughtExceptionHandler. The latter interface has a method handleException(), which the implementer overrides to take appropriate action, such as printing the stack trace to the console. As we'll see in a moment, we can actually install our own instance of UncaughtExceptionHandler to handle uncaught exceptions of a particular thread, or even for the whole system.
The specific procedure is as follows. When an uncaught exception occurs, the JVM does the following:
- it calls a special private method, dispatchUncaughtException(), on the Thread class in which the exception occurs;
- it then terminates the thread in which the exception occurred1.
The dispatchUncaughtException method, in turn, calls the thread's getUncaughtExceptionHandler() method to find out the appropriate uncaught exception handler to use. Normally, this will actually be the thread's parent ThreadGroup, whose handleException() method by default will print the stack trace.
However, we can actually override this process for an individual thread, for a ThreadGroup, or for all threads, as follows:
Thus, the full process used to determine which uncaught exception handler is called is shown in Figure 2 below:
Figure 2: Process by which Java decides on which uncaught exception handler to call for a given thread.
As mentioned above, care must be taken to prevent uncaught exceptions from terminating vital threads (or compensating for thread termination). On the next page, we look at a special case of uncaught exceptions in the event dispatch thread, where the Java runtime includes code to start a new event dispatch thread when the old one dies.
1. In some cases, you therefore have to be careful to make sure that an uncaught exception doesn't terminate an important thread unexpectedly. For example, an uncaught exception in the thread that handles JavaSound MIDI events will terminate the MIDI event handling thread and prevent further MIDI events from being processed.
http://www.javamex.com/tutorials/exceptions/exceptions_uncaught_handler.shtml
- How uncaught exceptions are handled
- 23.3 How exceptions are handled
- How most exceptions should be handled.
- Uncaught exceptions in scheduled tasks
- Uncaught Exceptions ----《Pro_Java_8_Programming_(3rd_edition)》
- Handling Uncaught and Unexpected Exceptions (C++)
- how old are 香港!
- How Classes are Found
- How Classes are Found
- How Classes are Found
- how are you
- Swift:Errors thrown from here are not handled because the enclosing catch is not exhaustive
- How I’ve handled multiple screen resolutions in my Unity3D game
- The following exceptions are thrown by certain C# operations.
- How are you doing, Jane?
- hello , how are you today?
- 怎样回答“How are you”?
- 怎样回答 How are you?
- 常见异常
- 如何一键导出MySQL数据库
- Queue小例子
- C/C++求对数
- JSP账号密码验证提示
- How uncaught exceptions are handled
- hdu1175
- JAVA的面向对象编程--------课堂笔记
- Win7优化加速[1][珍藏版]
- 黑马程序员——泛型
- tomcat虚拟目录配置
- 程序员面试题精选100题
- automake 搞定-感谢Vin Say
- PHP判断某字符串是否存在于字符串中