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块中的异常
*/
}
try-catch-finally陷阱
最新推荐文章于 2022-12-08 22:28:18 发布