try-catch-finally陷阱
2015-07-18 12:27
681 查看
package com.jjyy.test; import org.junit.Test; public class BasicTest { @Test public void test01() { System.out.println(test_01());//try } @Test public void test02() { System.out.println(test_02());//finally } @Test public void test03() { System.out.println(test_03());//catch } @Test public void test04() { System.out.println(test_04());//finally } @Test public void test05() { System.out.println(test_05());//java.lang.NumberFormatException } @Test public void test06() { System.out.println(test_06());//finally } @Test public void test07() { System.out.println(test_07());//java.lang.NumberFormatException } @Test public void test08() { System.out.println(test_08());//finally } @Test public void test09() { System.out.println(test_09());//java.lang.NullPointerException } public String test_01() { String t = ""; try { t = "try"; return t; } catch (Exception e) { // result = "catch"; t = "catch"; return t; } finally { t = "finally"; } } @SuppressWarnings("finally") public String test_02() { String t = ""; try { t = "try"; return t; } catch (Exception e) { // result = "catch"; t = "catch"; return t; } finally { t = "finally"; return t; } } public String test_03() { String t = ""; try { t = "try"; Integer.parseInt(null); return t; } catch (Exception e) { t = "catch"; return t; } finally { t = "finally"; // System.out.println(t); // return t; } } @SuppressWarnings("finally") public String test_04() { String t = ""; try { t = "try"; Integer.parseInt(null); return t; } catch (Exception e) { t = "catch"; return t; } finally { t = "finally"; return t; } } public String test_05() { String t = ""; try { t = "try"; Integer.parseInt(null); return t; } catch (Exception e) { t = "catch"; Integer.parseInt(null); return t; } finally { t = "finally"; //return t; } } @SuppressWarnings("finally") public String test_06() { String t = ""; try { t = "try"; Integer.parseInt(null); return t; } catch (Exception e) { t = "catch"; Integer.parseInt(null); return t; } finally { t = "finally"; return t; } } public String test_07() { String t = ""; try { t = "try"; Integer.parseInt(null); return t; } catch (NullPointerException e) { t = "catch"; return t; } finally { t = "finally"; } } @SuppressWarnings("finally") public String test_08() { String t = ""; try { t = "try"; Integer.parseInt(null); return t; } catch (NullPointerException e) { t = "catch"; return t; } finally { t = "finally"; return t; } } @SuppressWarnings("finally") public String test_09() { String t = ""; try { t = "try";return t; } catch (Exception e) { t = "catch"; return t; } finally { t = "finally"; String.valueOf(null); return t; } } /* 对以上所有的例子进行总结 1 try、catch、finally语句中,在如果try语句有return语句,则返回的之后当前try中变量此时对应的值,此后对变量做任何的修改,都不影响try中return的返回值 2 如果finally块中有return 语句,则返回try或catch中的返回语句忽略。 3 如果finally块中抛出异常,则整个try、catch、finally块中抛出异常 所以使用try、catch、finally语句块中需要注意的是 1 尽量在try或者catch中使用return语句。通过finally块中达到对try或者catch返回值修改是不可行的。 2 finally块中避免使用return语句,因为finally块中如果使用return语句,会显示的消化掉try、catch块中的异常信息,屏蔽了错误的发生 3 finally块中避免再次抛出异常,否则整个包含try语句块的方法回抛出异常,并且会消化掉try、catch块中的异常 */ }
相关文章推荐
- u3d 当你发现一个函数被调用,但是完全找不到是谁调用的时候
- My first CSDN
- hashCode与equals的区别与联系
- HYSBZ 1012 最大数maxnumber
- thinkphp的知识点
- 操作系统的启动
- codevs1026
- VC 各种情况下的窗口句柄的获取
- linux常用命令或功能
- 安卓的事件传递,中断,分发机制
- 高版本数据库还原低版本的bak
- Copy List with Random Pointer
- 数据预处理
- 【随笔】mvc使用forms身份验证实现登陆
- PHP+MYSQL如何插入记录到数据库
- kappa 一致性系数计算实例
- [debug]重定义默认参数
- 如何在自定义的ViewController加载Storyboard里面的ViewController
- centOS上安装redis
- 核间通信原理