Java 不要在finally块中处理返回値
不要在finally块中处理返回値
在finally代码块中使用return关键字时一定要慎重,finally代码块中的return返回值逻辑会直接覆 盖try代码块中正常的return返回值。
//不要在finally块中处理返回値
public class Demo {
public static void main(String[] args) {
System.out.println("count的返回值为:"+count());
}
public static int count() {
try {
return 1;
}catch (Exception e) {
// TODO: handle exception
}finally {
return -1;
}
}
}
点击加载更多评论>>