4 ms·
Try { return } finally {}
- Someone 10y agoclass 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); } } As before, the 'finally' clause runs after the return statement has been executed. That's clear, but does it affect the returned object? C# has value types (and Java is scheduled to get them), so it has a third variant of this code. Given the subtleties involved, I think a language should forbid this construct. However, I'm not sure that can be checked at compile time if the compiler has insudffient information about aliasing.