java - 异常处理的内部行为是什么?

标签 java exception

如果有异常,则创建一个异常对象。

如果异常没有被处理,那个异常对象会发生什么,内部机制是什么?

最佳答案

可以查看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.

enter image description here

关于java - 异常处理的内部行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438356/

相关文章:

java - jOOQ 不生成 <daos> 标志设置为 true 的 DAO

exception - 抛出 Exception 与抛出特定异常(如 NullPointerException)的区别

java - 需要在多线程环境中实现 ArrayList 的傻瓜式同步

java - TestForNull的 map 查找效率

php - php中openssl解密的异常处理

c# - Ping Pong - 超出范围异常

python - PyCharm 异常中断不适用于 Theano

c++ - 通过引用捕获(使用 catch)时异常对象的范围

java - RandomAccessFile Java - 复杂性

java - java中如何从图像中提取文本?