java异常语句中的finally块通常用来做资源释放操作,如关闭文件、关闭网络连接、关闭数据库连接等。正常情况下finally语句中不应该使用return语句也不应该抛出异常,以下讨论仅限于java语言设计本身,正常编码时应避免。
finally块中使用return会覆盖method的返回值
以下代码的返回值为:1
public static int div(){ try { return 3; }catch (ArithmeticException e){ System.out.println("catch in div"); return 2; } finally { System.out.println("finally in div"); return 1; } }
以下代码的返回值同样是:1
public static int div(){ try { return 3/0; }catch (ArithmeticException e){ System.out.println("catch in div"); return 2; } finally { System.out.println("finally in div"); return 1; } }
finally块中使用return会抑制异常的冒泡传输
即:只要finally中使用了return语句,调用者便认为该方法正常返回
以下代码的输出为
catch in div
finally in div
catch in adapter
finally in adapter
/** * Created by Administrator on 2017/11/27. */ public class Test { public static void main(String[] args) { adapter(); } public static void adapter() { try { div(); } catch (ArithmeticException e) { System.out.println("catch in adapter"); } finally { System.out.println("finally in adapter"); } } public static int div() { try { int a = 5 / 0; return a; } catch (ArithmeticException e) { System.out.println("catch in div"); throw e; // 重新将异常抛出给调用者 } finally { System.out.println("finally in div"); } } }
但如果在 div 的finally块中添加了return语句
public static int div(){ try { int a = 5/0; return a; }catch (ArithmeticException e){ System.out.println("catch in div"); throw e; // 重新将异常抛出给调用者,但是抛出会被忽略 } finally { System.out.println("finally in div"); return 1; } }
则代码的输出为
catch in div
finally in div
finally in adapter
即:finally块中的return语句会阻止异常的栈调用传输,使caller认为该方法已经正常返回
finally块中的throw语句会覆盖try和catch语句中的异常
以下代码
/** * Created by Administrator on 2017/11/27. */ public class Test { public static void main(String[] args) { adapter(); } public static void adapter() { try { div(); } catch (Exception e) { System.out.println(String.format("catch in adapter: %s",e.getMessage())); } finally { System.out.println("finally in adapter"); } } public static int div() throws Exception{ try { int a = 5 / 0; return a; } catch (ArithmeticException e) { System.out.println("catch in div"); throw new Exception("Exception in div"); // 抛出新的异常 } finally { System.out.println("finally in div"); throw new Exception("Exception in Finally"); // 抛出新的异常 } } }
输出是:
catch in div
finally in div
catch in adapter: Exception in Finally
finally in adapter
即,catch块中抛出的异常北finally块抛出的异常替换了
修改div方法为
public static int div() throws Exception{ try { int a = 5 / 0; return a; } finally { System.out.println("finally in div"); throw new Exception("Exception in Finally"); // 抛出新的异常 } }
输出为:
finally in div
catch in adapter: Exception in Finally
finally in adapter
即,try块中捕获的异常北finally块抛出的异常替换