Java异常处理(二)
2015-11-19 19:41
337 查看
Java异常处理(二)
Java基本的异常处理机制已经在上一篇的文章中写出,这篇文章主要分析在try{ ... }catch{ ... }代码块中加return语句后,对finally代码块的影响及对其调用者的影响。
下面看这段代码:
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; //会屏蔽Exception } } public static void main(String[] args) { TestException testException1 = new TestException(); try { testException1.testEx(); } catch (Exception e) { e.printStackTrace(); } } }
其运行结果如下:(和你想象的是否一致呢?)
i=2 i=1 testEx2, catch exception testEx2, finally; return value=false testEx1, finally; return value=false testEx, finally; return value=false
可以用Eclipse进行Step Into进行逐步调试,会有意想不到的效果。
在看看下面这段代码:
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; //会屏蔽Exception } } public static void main(String[] args) { TestException testException1 = new TestException(); try { testException1.testEx(); } catch (Exception e) { e.printStackTrace(); } } }
得到的运行结果如下:
i=2 i=1 testEx2, catch exception testEx2, finally; return value=false testEx1, catch exception testEx1, finally; return value=false testEx, finally; return value=false
对比以上两段代码,不难发现,两个的主要区别只是一行语句的差别在于testEx2方法下最后一句:return ret;
通过调试不难得出这样的结论:即使在方法内捕捉到异常,如果在finally代码段中加入了返回值,那么异常将会被返回值屏蔽。
相关文章推荐
- Java学习笔记--堆、栈、常量池
- 创建Java中的Pair类
- 人工鱼群算法java版
- Java中CountDownLatch、CyclicBarrier、Thread.join方法基本应用
- Jersey中Json与Java对象转换处理
- 解决:MyEclipse中SVN更新引起代码冲突导致部署失败
- java题目-1:输入20个学生的成绩,算出算出平均分和低于平均分的人数
- 几种 Java 序列化方案的性能比较
- Java 异常处理机制(一)
- highcharts+java
- 堆栈和队列的java实现
- java调用不在同一目录下的bat
- Spring并发访问的线程安全性问题
- javaweb学习总结(二十五)——jsp简单标签开发(一)
- java学习笔记03
- 简单项目开发之一(springmvc+log4j)
- spring里面怎么取容器里面的常量值
- java中的TreeSet集合
- 简单项目开发之一(springmvc+filter)
- Eclipse常见错误