图片来源于:下面的连接
https://mp.weixin.qq.com/s/7MbgphfBDBHWLx9nx5lHqA
https://mp.weixin.qq.com/s/7MbgphfBDBHWLx9nx5lHqA
阅读笔记:
1 如果finally 块中有 return 关键字,那么 try 块以及 catch 块中的 return 都将会失效,所以在开发的过程中不应该在 finally 块中写 return 语句。
2 如果在 finally 块中没有 return 语句,那么无论在 finally 代码块中是否修改返回值,返回值都不会改变,仍然是执行 finally 代码块之前的值。
finally 肯定会执行并且是输出的“333”,但是finally里面没有return语句所以返回值是 ‘222’
public static String test() {
String test = "111";
try {
test = "222";
return test;
} finally {
test = "333";
// 打印的是 333
System.out.println(test);
}
}
但是如果是下面这种写法,返回值为“333”,这也和第一条笔记相符合,当finally 又返回时try 和 catch 中的return失效:
public static String test() {
String test = "111";
try {
test = "222";
return test;
} finally {
test = "333";
// 打印的是 333
System.out.println(test);
return test;
}
}
3 try-with-resources 实际上就编译以后给我们加上了finally ,使用的时候需要资源实现 Closeable 接口。
4 异常需要分类别系统一异常和业务异常,每一个异常有一个code。