try-catch-finally陷阱

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块中的异常
	
	*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值