6 ms·
Relatedly, I don’t know if the following has already been fixed in more recent Java versions, but it should: final Foo foo; try { foo = bar(baz);
by layer8 4y ago
Relatedly, I don’t know if the following has already been fixed in more recent Java versions, but it should:
final Foo foo;
try
{
foo = bar(baz);
}
catch (SomeException ex)
{
foo = SOME_DEFAULT;
}
For some reason, the assignment in the catch clause is seen as a reassignment ("variable foo might already have been assigned"). As a workaround, one can factor it out into a separate method and have “return” there instead of “foo =“. This is particularly inconvenient in constructors when you want to assign to a final field.
- deleted 4y ago[deleted]
- SeanLuke 4y ago??? I just tried this on Java 8 and it compiles fine.
- layer8 4y agoSorry, I had slightly misremembered, please see my corrected comment above. The second assignment is seen as a reassignment when the variable or field is final.
- kaashif 4y agoI'm having trouble constructing a specific example that doesn't work, can you provide a minimal example that fails to compile? This compiles fine in Java 11, for example: import java.io.IOException; class Test { int g() throws IOException { throw new RuntimeException("aaa"); } int f() throws IOException { int x; try { x = g(); } catch (IOException e) { x = 3; } return x; } } So the compiler definitely believes that x is initialized because we can return it. Not a Java expert so maybe there's some subtlety I'm missing.
- layer8 4y agoSorry, I had slightly misremembered, please see my corrected comment above. The second assignment is seen as a reassignment when the variable or field is final.
- dzaima 4y agoIt's because if you had final Foo foo; try { foo = bar(baz); otherFunction(); } catch (SomeException ex) { foo = SOME_DEFAULT; } that'd truly be invalid - the first assignment could run, otherFunction errors, and foo gets reassigned. Even if SomeException is a checked exception that otherFunction doesn't declare in its throws, it's still possible to get the jvm to throw it, and things do rely on that - see Lombok @SneakyThrows. So the only case that such final field assignment can work is if it's the last evaluated expression in the try case (plus some guaranteed non-throwing arithmetic or boring assignments), which is quite the weird thing to have as specified behavior in a language.
- vbezhenar 4y agoWhat's non-boring assignment? Every assignment in Java is boring. This is just bad flow analyzer and that's about it.
- dzaima 4y agosomething like final Foo foo; try { foo = bar(baz); Integer a = null; int b = a; } catch ... (though that's less the assignment, more implicit behavior; point being, you've got quite the restrictive environment after "foo =")
- vbezhenar 4y agoAccepting that kind of code probably is too much to ask. But accepting original example should be do-able with pretty simple AST pattern matching.
- dzaima 4y agoRight. Not saying that it'd be hard to support the original code, but more that it's just an extremely weird case to have. As in, I'd say that adding "foo = try { bar(baz) } catch (SomeException ex) { SOME_DEFAULT }", even though that requires adding the ability for blocks to "return" a value, would be a more sensible change to aid the problem at hand. Were the original to be allowed, it be the first case where adding a System.out.println(); statement into code {}s results in an error. (ok, second case, the first being adding it before a super()/this() in a constructor. But hey look what post we're on!)
- int_19h 4y agoThis is why the statement/expression dichotomy is a bad idea. When try/catch is an expression just like everything else, the problem disappears - you compute however you want, and then assign once.