如果有异常,则创建一个异常对象。
如果异常没有被处理,那个异常对象会发生什么,内部机制是什么?
最佳答案
可以查看How uncaught exceptions are handled :
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.
关于java - 异常处理的内部行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438356/