java中finally与return_Java中finally和return的关系实例解析

本文研究的主要是Java中finally和return的关系,具体介绍和实例如下所示。

finally 和 return 关系的总结

1.try块中有System.exit(0)这样的语句,由于它是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。

2.其它情况下,finally语句都必然会被执行。因此可以在这里执行一些资源的释放操作。

(1)finally中的return会覆盖try/catch中的renturn。

(2)在finally中写return语句会有警告,因为它会阻止函数抛出异常,而改为正常返回。

package com.demo.test;

public class FinallyAndReturn {

private static void finallyAndTryReturn() {

try {

System.out.println("finallyAndTryReturn -> try");

return;

}

catch (Exception e) {

System.out.println("finallyAndTryReturn -> catch");

}

finally {

System.out.println("finallyAndTryReturn -> finally");

}

}

private static void finallyAndCatchReturn() {

try {

System.out.println("finallyAndCatchReturn -> try");

throw new Exception();

}

catch (Exception e) {

System.out.println("finallyAndCatchReturn -> catch");

return;

}

finally {

System.out.println("finallyAndCatchReturn -> finally");

}

}

// finally语句是在try的return语句执行之后,return返回之前执行。

private static String tryReturn() {

String str = "initialized";

try {

System.out.println("tryReturn -> try");

str = "try";

return str;

}

catch (Exception e) {

System.out.println("tryReturn -> catch");

str = "catch";

}

finally {

System.out.println("tryReturn -> finally");

str = "finally";

}

return null;

}

private static String tryReturnAndFinallyReturn() {

String str = "initialized";

try {

System.out.println("tryReturnAndFinallyReturn -> try");

str = "try";

return str;

}

catch (Exception e) {

System.out.println("tryReturnAndFinallyReturn -> catch");

str = "catch";

}

finally {

System.out.println("tryReturnAndFinallyReturn -> finally");

/*

* Warning: finally block does not complete normally

* 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。

*/

str = "finally";

return str;

}

}

private static String tryThrowAndFinallyReturn() throws Exception {

String str = "initialized";

try {

System.out.println("tryThrowAndFinallyReturn -> try");

str = "try";

throw new Exception();

}

catch (Exception e) {

System.out.println("tryThrowAndFinallyReturn -> catch");

str = "catch";

throw new Exception();

}

finally {

System.out.println("tryThrowAndFinallyReturn -> finally");

/*

* Warning: finally block does not complete normally

* 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。

*/

str = "finally";

return str;

}

}

private static void finallyAndRuntimeException() {

try {

System.out.println("finallyAndRuntimeException -> try");

throw new RuntimeException();

}

catch (Exception e) {

System.out.println("finallyAndRuntimeException -> catch");

}

finally {

System.out.println("finallyAndRuntimeException -> finally");

}

}

private static void finallyAndExit() {

try {

System.out.println("finallyAndExit -> try");

// System.exit(0);是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。

System.exit(0);

}

catch (Exception e) {

System.out.println("finallyAndExit -> catch");

}

finally {

System.out.println("finallyAndExit -> finally");

}

}

public static void main(String[] args) {

finallyAndTryReturn();

System.out.println();

finallyAndCatchReturn();

System.out.println();

System.out.println("tryReturn return -> " + tryReturn());

System.out.println();

System.out.println("tryReturnAndFinallyReturn return -> " + tryReturnAndFinallyReturn());

System.out.println();

try {

System.out.println("tryThrowAndFinallyReturn return -> " + tryThrowAndFinallyReturn());

}

catch (Exception e) {

e.printStackTrace();

}

System.out.println();

finallyAndRuntimeException();

System.out.println();

finallyAndExit();

}

}

演示结果:

a6f582c953ce4b520dc97e5cbcbd61c7.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值