1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > JAVA try...catch...finally中的执行顺序和return语句

JAVA try...catch...finally中的执行顺序和return语句

时间:2021-02-23 20:09:38

相关推荐

JAVA try...catch...finally中的执行顺序和return语句

public static int get() {

try {

System.out.println("try");

return 1;

//throw new Exception();

} catch (Exception e) {

System.out.println("catch");

return 2;

} finally {

System.out.println("finally");

return 3;

}

} 结果: try finally 3

public static int get() {

try {

System.out.println("try");

//return 1;

throw new Exception();

} catch (Exception e) {

System.out.println("catch");

return 2;

} finally {

System.out.println("finally");

return 3;

}

} 结果: try catch finally 3

public static int get() {

try {

System.out.println("try");

//return 1;

throw new Exception();

} catch (Exception e) {

System.out.println("catch");

return 2;

} finally {

System.out.println("finally");

//return 3;

}

} 结果: try catch finally 2结论:finally块中的return会覆盖掉try或catch块中的return

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。