java异常详细.docVIP

  • 1
  • 0
  • 约1.8万字
  • 约 22页
  • 2018-03-31 发布于湖北
  • 举报
java异常详细.doc

? 深入理解java异常处理机制 /hguisu/article/details/6155636 ?1. 引子 ? ? ? ?try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解。不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单、听话。不信?那你看看下面的代码,“猜猜”它执行后的结果会是什么?不要往后看答案、也不许执行代码看真正答案哦。如果你的答案是正确,那么这篇文章你就不用浪费时间看啦。 [java]?view plaincopyprint? span?style=background-color:?rgb(255,?255,?255);package?Test;?? ?? public?class?TestException?{?? ????public?TestException()?{?? ????}?? ?? ????boolean?testEx()?throws?Exception?{?? ????????boolean?ret?=?true;?? ????????try?{?? ????????????ret?=?testEx1();?? ????????}?catch?(Exception?e)?{?? ????????????System.out.println(testEx,?catch?exception);?? ????????????ret?=?false;?? ????????????throw?e;?? ????????}?finally?{?? ????????????System.out.println(testEx,?finally;?return?value=?+?ret);?? ????????????return?ret;?? ????????}?? ????}?? ?? ????boolean?testEx1()?throws?Exception?{?? ????????boolean?ret?=?true;?? ????????try?{?? ????????????ret?=?testEx2();?? ????????????if?(!ret)?{?? ????????????????return?false;?? ????????????}?? ????????????System.out.println(testEx1,?at?the?end?of?try);?? ????????????return?ret;?? ????????}?catch?(Exception?e)?{?? ????????????System.out.println(testEx1,?catch?exception);?? ????????????ret?=?false;?? ????????????throw?e;?? ????????}?finally?{?? ????????????System.out.println(testEx1,?finally;?return?value=?+?ret);?? ????????????return?ret;?? ????????}?? ????}?? ?? ????boolean?testEx2()?throws?Exception?{?? ????????boolean?ret?=?true;?? ????????try?{?? ????????????int?b?=?12;?? ????????????int?c;?? ????????????for?(int?i?=?2;?i?=?-2;?i--)?{?? ????????????????c?=?b?/?i;?? ????????????????System.out.println(i=?+?i);?? ????????????}?? ????????????return?true;?? ????????}?catch?(Exception?e)?{?? ????????????System.out.println(testEx2,?catch?exception);?? ????????????ret?=?false;?? ????????????throw?e;?? ????????}?finally?{?? ????????????System.out.println(testEx2,?finally;?return?value=?+?ret);?? ????????????return?ret;?? ????????}?? ????}?? ?? ????public?static?void?main(String

文档评论(0)

1亿VIP精品文档

相关文档