java中try、catch、finally返回语句执行顺序
2016-09-05 22:08
459 查看
(1)如果catch块中有返回语句,则先执行finally语句块中的代码。
输出结果:
finally
catch
(2)如果catch块和finally块中均有返回语句,则finally块中的返回语句会覆盖catch块中的返回语句,即catch块执行到return语句之前会执行finally块中的代码。
finally
(3)如果catch块中有System.exit(0)语句,则直接退出程序。
输出结果:
exit之前
public class Main { public static void main(String[] args) { System.out.println(test()); } public static String test(){ try{ int i=10/0; }catch(Exception e){ return "catch"; }finally{ System.out.println("finally"); } return null; } }
输出结果:
finally
catch
(2)如果catch块和finally块中均有返回语句,则finally块中的返回语句会覆盖catch块中的返回语句,即catch块执行到return语句之前会执行finally块中的代码。
public static String test(){ try{ int i=10/0; }catch(Exception e){ return "catch"; }finally{ return "finally"; } }输出结果:
finally
(3)如果catch块中有System.exit(0)语句,则直接退出程序。
public static String test(){ try{ int i=10/0; }catch(Exception e){ System.out.println("exit之前"); System.exit(0); System.out.println("exit之后"); return "catch"; }finally{ return "finally"; }
输出结果:
exit之前
相关文章推荐
- 【Java】try-catch-finally语句中return的执行顺序思考
- Java-try_catch_finally中带有return语句的执行顺序
- java 中 try catch finally 语句的执行顺序
- Java异常捕获之try-catch-finally-return的执行顺序
- Java try catch finally的执行顺序
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- return语句在try-catch-finally中的执行顺序
- 关于java中try catch finally的执行顺序
- try...catch...finally中的执行顺序和return语句 总结
- 黑马程序员 try或catch中存在return 语句时,它们与finally 的执行顺序问题
- 含有return语句的try-catch-finally语句执行顺序
- Java异常捕获之try-catch-finally-return的执行顺序
- Java try catch finally的执行顺序
- Java中try catch finally语句中含有return语句的执行情况
- try catch finally中return语句与非return语句的执行顺序问题
- java try、catch、finally及finally执行顺序详解
- Java try、catch、finally及finally执行顺序详解
- java try、catch、finally及finally执行顺序详解
- java中try catch finally与return的执行顺序