TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Try { return } finally {}

6 点作者 Halienja超过 8 年前

1 comment

Someone超过 8 年前
<p><pre><code> class Test { public int foo; public Test aaa() { Test x = new Test(); x.foo = 1; try { return x; } catch (Exception e) { } finally { ++x.foo; } return x; } public static void main(String[] args) { Test t = new Test(); Test y = t.aaa(); System.out.println(y.foo); } } </code></pre> As before, the &#x27;finally&#x27; clause runs after the return statement has been executed. That&#x27;s clear, but does it affect the returned object?<p>C# has value types (and Java is scheduled to get them), so it has a third variant of this code.<p>Given the subtleties involved, I think a language should forbid this construct. However, I&#x27;m not sure that can be checked at compile time if the compiler has insudffient information about aliasing.