TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Try { return } finally {}

6 pointsby Halienjaover 8 years ago

1 comment

Someoneover 8 years ago
<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.