Java 异常丢失及finally子句
2013-09-10 00:00
190 查看
几天前在Stack overflow上看到一个题:
I have a small theoretical problem with try-catch constructions.
I took a practical exam yesterday about Java and I don't understand following example:
The question was "what the output will look like?"
I was pretty sure it would be AB2C3, BUT suprise suprise, it's not true.
The right answer is ABC3 (tested and really it's like that).
My question is, where did the Exception("2") go?
我以为因为finally子句始终会执行,因此在抛出Exception("3");的时候将catch中的异常覆盖了,今天突然又想起来到底catch中的异常是否还存在呢,搜了一下,csdn上飞天金刚给出了一个分析:
http://blog.csdn.net/sureyonder/article/details/5560538
自己写了一个其实跟上面的一样
Exception table:
from to target type
0 18 18 Class java/lang/IllegalArgumentException
0 38 37 any
0 56 56 Class java/lang/IndexOutOfBoundsException
三个exception都在table里面,只不过抛出的只是最后一个而已。其他两个都被java当做垃圾给扔了,而且第二个没有被catch的exception类型未知。exception table是什么原理呢?有待查询。
I have a small theoretical problem with try-catch constructions.
I took a practical exam yesterday about Java and I don't understand following example:
try { try { System.out.print("A"); throw new Exception("1"); } catch (Exception e) { System.out.print("B"); throw new Exception("2"); } finally { System.out.print("C"); throw new Exception("3"); } } catch (Exception e) { System.out.print(e.getMessage()); }
The question was "what the output will look like?"
I was pretty sure it would be AB2C3, BUT suprise suprise, it's not true.
The right answer is ABC3 (tested and really it's like that).
My question is, where did the Exception("2") go?
我以为因为finally子句始终会执行,因此在抛出Exception("3");的时候将catch中的异常覆盖了,今天突然又想起来到底catch中的异常是否还存在呢,搜了一下,csdn上飞天金刚给出了一个分析:
http://blog.csdn.net/sureyonder/article/details/5560538
自己写了一个其实跟上面的一样
try { try{ System.out.println("b"); throw new IllegalArgumentException("2"); }catch(IllegalArgumentException e) { System.out.println("c"); throw new UnsupportedOperationException("3"); }finally { System.out.println("d"); throw new IndexOutOfBoundsException("4"); } }catch(IndexOutOfBoundsException e) { System.out.println(e.getMessage()); e.printStackTrace(); }查看了一下程序的ExceptionTable
Exception table:
from to target type
0 18 18 Class java/lang/IllegalArgumentException
0 38 37 any
0 56 56 Class java/lang/IndexOutOfBoundsException
三个exception都在table里面,只不过抛出的只是最后一个而已。其他两个都被java当做垃圾给扔了,而且第二个没有被catch的exception类型未知。exception table是什么原理呢?有待查询。
相关文章推荐
- java finally 造成的异常丢失
- JAVA finally字句的异常丢失和返回值覆盖解析
- JAVA中的finally及异常丢失问题
- 从头认识java-10.6 finally(3)-异常的丢失
- 详解Java异常处理中finally子句的运用
- JAVA finally字句的异常丢失和返回值覆盖解析
- java异常丢失及异常链
- java里面try,catch,finally异常捕捉
- R.java was modified manually! Reverting to generated version!(R文件丢失异常原因汇总)
- java异常处理-finally中使用return和throw语句
- java异常栈信息丢失
- java 异常丢失
- Java中异常处理-finally和return执行顺序
- JAVA学习笔记25——异常机制1:Exception简介+异常处理的try_catch_finally方法
- Java - Java语言如何进行异常处理,关键字:throws、throw、try、catch、finally分别如何使用?
- java中异常处理finally和return语句的执行顺序
- java异常语句中catch,finally的执行顺序
- 0016 Java学习笔记-异常-如果try-catch-finally中都存在return语句会怎样?
- JAVA异常处理机制的个人理解(try catch throw finally)
- Java异常捕获之try-catch-finally-return的执行顺序