<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
package com.ruoyi.web.controller.platform;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TryCatchFinally {
private final static Logger logger = LoggerFactory.getLogger(TryCatchFinally.class);
public String test1() {
String t = "";
try {
t = "try";
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
}
}
@Test
public void result1() {
logger.info("=============test1返回结果:{}===============", test1());
}
public String test2() {
String t = "";
try {
t = "try";
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
@Test
public void result2() {
logger.info("=============test2返回结果:{}===============", test2());
}
public String test3() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
logger.info("########finally的值是{}###########", t);
}
}
@Test
public void result3() {
logger.info("=============test3返回结果:{}===============", test3());
}
public String test4() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
@Test
public void result4() {
logger.info("=============test4返回结果:{}===============", test4());
}
public String test5() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
Integer.parseInt(null);
return t;
} finally {
t = "finally";
}
}
@Test
public void result5() {
logger.info("=============test5返回结果:{}===============", test5());
}
public String test6() {
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;
}
}
@Test
public void result6() {
logger.info("=============test6返回结果:{}===============", test6());
}
public String test7() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
}
}
@Test
public void result7() {
logger.info("=============test7返回结果:{}===============", test7());
}
public String test8() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
@Test
public void result8() {
logger.info("=============test8返回结果:{}===============", test8());
}
public String test9() {
String t = "";
try {
t = "try";
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
String.valueOf(null);
return t;
}
}
@Test
public void result9() {
logger.info("=============test9返回结果:{}===============", test9());
}
}
对以上所有的例子进行总结
- try、catch、finally语句中,在如果try语句有return语句,则返回的之后当前try中变量此时对应的值,此后对变量做任何的修改,都不影响try中return的返回值
- 如果finally块中有return 语句,则返回try或catch中的返回语句忽略。
- 如果finally块中抛出异常,则整个try、catch、finally块中抛出异常
所以使用try、catch、finally语句块中需要注意的是
- 尽量在try或者catch中使用return语句。通过finally块中达到对try或者catch返回值修改是不可行的。
- finally块中避免使用return语句,因为finally块中如果使用return语句,会显示的消化掉try、catch块中的异常信息,屏蔽了错误的发生
- finally块中避免再次抛出异常,否则整个包含try语句块的方法回抛出异常,并且会消化掉try、catch块中的异常