Always finally.java_关于java:我们可以在finally块中使用“return”

本问题已经有最佳答案,请猛点这里访问。

我们可以在finally块中使用return语句。 这会导致任何问题吗?

您也可以阅读这篇文章

从finally块内部返回将导致exceptions丢失。

finally块中的return语句将导致可能在try或catch块中抛出的任何异常被丢弃。

根据Java语言规范:

If execution of the try block completes abruptly for any other reason

R, then the finally block is executed, and then there is a choice:

If the finally block completes normally, then the try statement

completes  abruptly for reason R.

If the finally block completes abruptly for reason S, then the try

statement  completes abruptly for reason S (and reason R is

discarded).

注意:根据JLS 14.17 - 返回语句总是突然完成。

JLS报价不支持您对finally块内的return语句的声明?

来自JLS的这些行用于异常,但对于return语句也是如此。

我仍然没有得到引用和你的语句之间的关系 - 所以我写了一段代码,表明当finally块包含return语句时,从catch块抛出的异常会被吃掉,但是从try块中抛出的异常抓住。请验证。

浏览此代码ideone.com/AU9KvS。尝试的例外情况被吃掉了。

没有阻挡块我完全错过了变种!谢谢,已经upvoted :)

Java语言规范明确了返回:return语句总是突然完成。 (参考:thegreyblog.blogspot.co.uk/2011/02/)

您应该在答案中添加return被Java规范视为突然完成。

@kolobok添加了注释。

是的,您可以在finally块中编写return语句,它将覆盖其他返回值。

编辑:

例如,在下面的代码中

public class Test {

public static int test(int i) {

try {

if (i == 0)

throw new Exception();

return 0;

} catch (Exception e) {

return 1;

} finally {

return 2;

}

}

public static void main(String[] args) {

System.out.println(test(0));

System.out.println(test(1));

}

}

输出总是2,因为我们从finally块返回2。记住,finally总是执行是否存在异常。所以当finally块运行时,它将覆盖其他的返回值。在finally块中编写return语句不是必需的,实际上你不应该写它。

你能解释一下吗?

try {throws new exception(); } catch {throws new exception();} finally {return 5;这里发生了什么?

请参阅编辑

@Rakesh它返回5

如果从try或catch块完成System.exit(0)调用,它可能无法正常工作。在这种情况下,控件不会最终阻止。

无论返回什么,System.exit(0)都会阻止执行finally块,因为JVM将关闭

@RakeshKR它将返回5,因为catch块抛出的异常它将由finally块处理,如果你删除finally块所以在给定的情况下它将给出编译时错误,所以你要么添加finally块或明确你将需要处理它。

是的,你可以,但你不应该1,因为finally块是出于特殊目的。

finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

不建议在其中编写逻辑。

这并没有真正解释得太多。 TJ Crowder的答案要好得多 - stackoverflow.com/questions/15225819/

@BoratSagdiyev如果它太详细了..请去看看它。谢谢

您可以在finally块中写入return语句,但是从try块返回的值将在堆栈上更新,而不是finally块返回值。

我们假设你有这个功能

private Integer getnumber(){

Integer i = null;

try{

i = new Integer(5);

return i;

}catch(Exception e){return 0;}

finally{

i = new Integer(7);

System.out.println(i);

}

}

你从main方法调用它

public static void main(String[] args){

System.out.println(getNumber());

}

这打印

7

5

问题是我们是否可以从finally块返回一个值。记住finally语句中的return语句将覆盖其他返回值。请考虑以下代码片段:class ReturnClass {public int testValue(){try {return 3; } catch(Exception e){} finally {return 5; public static void main(String ar []){ReturnClass rc = new ReturnClass(); System.out.println(rc.testValue());}}输出将始终为5。

这是偏离主题的。它让我困惑,直到我仔细阅读你不是return来自finally的控件,而只是从那里打印一些东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值