Java异常处理(二)
来源:互联网 发布:2017淘宝发展趋势 编辑:程序博客网 时间:2024/05/18 04:01
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=2i=1testEx2, catch exceptiontestEx2, finally; return value=falsetestEx1, finally; return value=falsetestEx, 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=2i=1testEx2, catch exceptiontestEx2, finally; return value=falsetestEx1, catch exceptiontestEx1, finally; return value=falsetestEx, finally; return value=false
对比以上两段代码,不难发现,两个的主要区别只是一行语句的差别在于testEx2方法下最后一句:return ret;
通过调试不难得出这样的结论:即使在方法内捕捉到异常,如果在finally代码段中加入了返回值,那么异常将会被返回值屏蔽。
0 0
- java异常处理(二)
- Java异常处理(二)
- Java异常处理(二)
- java 异常处理(二)
- JAVA第十四弹(异常处理二)
- java中的异常处理(二)
- Java异常的处理机制(二)
- Java程序设计(二)异常处理机制
- Java基础-异常处理(二)
- 二:Java之异常处理
- JAVA异常处理二--finally
- java的异常处理(二)
- 异常处理(二)
- JAVA【异常二】异常处理机制
- Java基础之(三十六)Java异常处理<二>
- 一大波Java来袭(二)异常处理
- Java SE 基础概述(二)异常处理
- Java 异常的捕获与处理详解(二)
- ios编程富文本
- ScrollView嵌套ListView的问题,
- 利用Hog特征和SVM分类器进行行人检测
- Resid 学习笔记1-基础命令
- 每天半小时学英语
- Java异常处理(二)
- 每天一点python——类
- Python爬虫笔记一
- 什么是 未定义行为
- C++面向对象高级开发_极客班_第二节课笔记
- 三.指南针和磁力计
- 图灵测试
- Gradle学习手册
- if