标签:异常 执行 学习 his exception orm return catch stat
先说结论:
一般逻辑,先执行try语句;如果有异常,则继续执行catch语句;如果有finally语句,则执行finally语句;
任何执行try 或者catch中的return语句之前,如果finally存在的,都会先执行finally语句;
如果finally中有return语句,那么程序就在finally中return了,所以finally中的return是一定会被return的,否则
源码示例:
public class TryCatchTest { public static String testNormalWithFinally(){ try { int i = 0/1; System.out.println("this is try block"); return "return try"; } catch (Exception e) { e.printStackTrace(); System.out.println("this is catch block"); return "return catch"; } finally { System.out.println("this is finally block"); return "return finally "; } } public static String testExceptionWithFinally(){ try { int j = 1/0; System.out.println("this is ctry block"); return "return try"; } catch (Exception e) { e.printStackTrace(); System.out.println("this is catch block"); return "return catch"; } finally { System.out.println("this is finally block"); return "return finally "; } } public static String testNormalWithoutFinallyReturn(){ try { int i = 0/1; System.out.println("this is try block"); return "return try"; } catch (Exception e) { e.printStackTrace(); System.out.println("this is catch block"); return "return catch"; } finally { System.out.println("this is finally block"); } } public static String testExceptionWithoutFinallyReturn(){ try { int j = 1/0; System.out.println("this is ctry block"); return "return try"; } catch (Exception e) { e.printStackTrace(); System.out.println("this is catch block"); return "return catch"; } finally { System.out.println("this is finally block"); } } public static void main(String[] args) { System.out.println("-------------testNormalWithFinally-------------------"); System.out.println(testNormalWithFinally()); System.out.println("----------------testExceptionWithFinally----------------"); System.out.println(testExceptionWithFinally()); System.out.println("---------------testNormalWithoutFinallyReturn-----------------"); System.out.println(testNormalWithoutFinallyReturn()); System.out.println("--------------testExceptionWithoutFinallyReturn------------------"); System.out.println(testExceptionWithoutFinallyReturn()); } } --------------------------------日志输出--------------------------------------------- -------------testNormalWithFinally------------------- java.lang.ArithmeticException: / by zero this is try block at interview.trycatchfinally.TryCatchTest.testExceptionWithFinally(TryCatchTest.java:32) this is finally block return finally at interview.trycatchfinally.TryCatchTest.main(TryCatchTest.java:80) ----------------testExceptionWithFinally---------------- java.lang.ArithmeticException: / by zero this is catch block this is finally block return finally at interview.trycatchfinally.TryCatchTest.testExceptionWithoutFinallyReturn(TryCatchTest.java:63) ---------------testNormalWithoutFinallyReturn----------------- this is try block at interview.trycatchfinally.TryCatchTest.main(TryCatchTest.java:84) this is finally block return try --------------testExceptionWithoutFinallyReturn------------------ this is catch block this is finally block return catch
Java深入学习23:try catch finally执行和返回逻辑
标签:异常 执行 学习 his exception orm return catch stat
原文地址:https://www.cnblogs.com/wobuchifanqie/p/12764337.html