从 Java 中的 finally 块返回 [英] Returning from a finally block in Java

查看:29
本文介绍了从 Java 中的 finally 块返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近惊讶地发现在 Java 的 finally 块中可以有 return 语句.

I was surprised recently to find that it's possible to have a return statement in a finally block in Java.

似乎很多人认为按照不要在 finally 子句中返回'.再深入一点,我还发现了Java 的返回并不总是' 这显示了 finally 块中其他类型的流控制的一些非常可怕的例子.

It seems like lots of people think it's a bad thing to do as described in 'Don't return in a finally clause'. Scratching a little deeper, I also found 'Java's return doesn't always' which shows some pretty horrible examples of other types of flow control in finally blocks.

所以,我的问题是,谁能给我一个例子,其中 finally 块中的 return 语句(或其他流控制)产生更好/更易读的代码?

So, my question is, can anyone give me an example where a return statement (or other flow control) in a finally block produces better / more readable code?

推荐答案

您提供的示例足以从 finally 开始使用流程控制.

The examples you provided are reason enough to not use flow-control from finally.

即使有一个人为的例子说明它更好",也要考虑到后来必须维护您的代码并且可能不知道其中的微妙之处的开发人员.那个可怜的开发者甚至可能就是你......

Even if there's a contrived example where it's "better," consider the developer who has to maintain your code later and who might not be aware of the subtleties. That poor developer might even be you....

这篇关于从 Java 中的 finally 块返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆