News for hackers. Stuff that matters.
Re:JVM exceptions are weird: a decompiler perspective by friendzis on Monday November 10, 2025 @02:30AM (#45873378)
I would not call this snippet particularly "cursed". There is no "aktshchually this happens, therefore this is activated" hidden magic going on. The try-catch-finally construct is doing exactly what it is designed and documented to do: finally block is executed regardless of the outcome within try. The purpose of finally block is to fire regardless of exceptionality in control flow.

Surprising at first? Maybe. Cursed? Wouldn't say so. It is merely unconventional use of the construct.

Re:JVM exceptions are weird: a decompiler perspective by Joker_vD on Monday November 10, 2025 @07:34AM (#45875327)
It messes with the semantics of "return" statement. The conventional intuition is that right after a "return" statement completes, the current method's invocation ends and the control returns to the caller. Unfortunately, the actual semantics has to be that is "attempts to return control":

    The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any try statements (§14.20) within the method or constructor whose try blocks or catch clauses contain the return statement, then any finally clauses of those try statements will be executed, in order, innermost to outermost, before control is transferred to the invoker of the method or constructor. Abrupt completion of a finally clause can disrupt the transfer of control initiated by a return statement.
This, I believe, is the only way for "return E", after the evaluation of E completes normally, to not return the E's value to the caller. Thanks for a needless corner-case complication, I guess.
Re:JVM exceptions are weird: a decompiler perspective by xxs on Monday November 10, 2025 @07:45AM (#45875416)
finally stuff is executed prior to the 'return'; effectively it's placed before the 'return' statement. So it's quite obvious.
Re:JVM exceptions are weird: a decompiler perspective by Joker_vD on Monday November 10, 2025 @08:18AM (#45875684)
No, it's not executed before the return, it's executed in the middle of it, so to speak:

    class Main {
        static int f() { System.out.println("f()"); return 1; }
        static int g() { System.out.println("g()"); return 2; }

        static int h() {
            try {
                return f();
            } finally {
                return g();
            }
        }

        public static void main(String[] args) {
            System.out.println(h());
        }
    }
will print

    f()
    g()
    2
If "finally" block was executed before the "return" in the "try", the call to f() would not have been made.
Re:JVM exceptions are weird: a decompiler perspective by xxs on Monday November 10, 2025 @12:20PM (#45878201)
Yes, very much is... return is not a single statement or a single opcode. the call to f() is returned on the stack, that's assigned to some local variable via istore, there is no 'ireturn', yet. Then the g() method is called along with whatever is printed, and the next instruction can be ireturn. which would return '2' indeed.
Re:JVM exceptions are weird: a decompiler perspective by Joker_vD on Monday November 10, 2025 @12:38PM (#45878427)
> return is not a single statement.

It is a single statement. It's just that executing it contains of two parts: evaluating the expression, and the non-local transfer of control ― and the weird part is that this control transfer can be hijacked. And it is weird, you can't do this with e.g. "if": if the test condition is evaluated successfully, then the control will go into one of the branches, and you can't mess with that... but with "return", you can abort this transfer of control. Which is, again, weird: all other expression-evaluating statements can go haywire only if the evaluation of the expression inside them throws an exception — but if that doesn't happen, the control transfer is completely trivial, with no special rules about try-catch blocks.

Re:JVM exceptions are weird: a decompiler perspective by xxs on Monday November 10, 2025 @01:22PM (#45879008)
you cut the statement in the middle, it's very obviously in terms of bytecode as we talk about bytecode generation.
Re:JVM exceptions are weird: a decompiler perspective by Joker_vD on Monday November 10, 2025 @01:36PM (#45879188)
Mo, we were talking about what would be reasonable semantics for the interaction of two features in a high-level programming language Java; the semantics of bytecode doesn't really matter in this case. The precedent for that would be that "finally"m in fact, really doesn't mesh all that well with what is available for bytecode, so javac has to resort to code duplication.
Re:JVM exceptions are weird: a decompiler perspective by deepsun on Saturday November 15, 2025 @06:39PM (#45941476)
> control transfer can be hijacked.

But useful. Similar to Go's "defer".

Re:JVM exceptions are weird: a decompiler perspective by Joker_vD on Wednesday November 19, 2025 @10:09AM (#45980491)
Go's "defer" can't prevent return from actually returning from the function. Most of the time, it can't change the computed return value either, unless you go out of your way and use named return values. Go's "defer" also can't do crimes with "break" and "continue".

So Go's "defer" has more "and also do some things on the way out" semantics than "you suddenly forget why you were going to leave the kitchen, so you don't".

Re:JVM exceptions are weird: a decompiler perspective by friendzis on Monday November 10, 2025 @10:42AM (#45876992)
> It messes with the semantics of "return" statement.

Yes, that's what exceptions and their handling handling does - it messes with control flow.

> The conventional intuition is that right after a "return" statement completes, the current method's invocation ends and the control returns to the caller.

It is part of similar conventional thinking that "a method call must return back to the caller on termination", which is not even applicable to Java and other languages with conventional exceptions.

> Thanks for a needless corner-case complication, I guess.

But what is the alternative otherwise? Not execute finally block if try block exits normally? Execute finally block, but ignore control flow statements? What if code in finally block throws? I maintain that execution of finally block, including control flow statements within, normally is the only sane behavior.

Re:JVM exceptions are weird: a decompiler perspective by Joker_vD on Monday November 10, 2025 @11:02AM (#45877209)
> It is part of similar conventional thinking that "a method call must return back to the caller on termination".

No, it's a part of a conventional thinking that "if a return statement executes without any exceptions thrown in the process, then the control will return from the current invocation". It's an extension of a conventional thinking "if an e.g. assignment statement executes without any exceptions thrown in the process, the control will go to the next statement". Really helps with reasoning about the program behaviour without spending an undue amount of brainpower.

> But what is the alternative otherwise?

Disallow "return", "break", "continue", and "goto" statements inside "finally" blocks, like C# does. Nobody sane misses this functionality anyway.

Re:JVM exceptions are weird: a decompiler perspective by alpinisme on Monday November 10, 2025 @07:37AM (#45875352)
Sufficiently unexpected (surprising) is cursed. If anything, that’s the way most people use that term for programming language constructs (weird coercions in JavaScript, conflicting naming conventions in PHP, overflow checks that get erased by the compiler due to UB in c/c++, etc.)
Re:JVM exceptions are weird: a decompiler perspective by friendzis on Monday November 10, 2025 @09:56AM (#45876590)
> Sufficiently unexpected (surprising) is cursed.

I think there three distinct classes of surprising that fall under this umbrella: 1. a behavior overrides another behavior due to some precedence rules 2. behavior of a construct in language x is different than behavior of similar construct in most mainstream languages 3. who in their right mind would design the thing to behave like this.

Examples could be: string "arithmetic" in most dynamically typed languages, PHP's ternary operator order of precedence and Python's handling of default function arguments. In all of these cases when you sit down, think about it and ask yourself "what's the alternative?", there's always a very reasonable answer to it, therefore, I think it is reasonable to call these behaviors cursed.

In this case the surprise factor comes in, because we are used to equating finally block with cleanup and I concur that many would trip on this the first time. But if you gave this exercise some time and asked "what should happen if finally block contains control flow statements?" the reasonable answer should be "take precedence", because the behavior would be surprising/cursed otherwise.

That's my reasoning.

Re:JVM exceptions are weird: a decompiler perspective by Akronymus on Monday November 10, 2025 @11:07AM (#45877279)
> In this case the surprise factor comes in, because we are used to equating finally block with cleanup and I concur that many would trip on this the first time. But if you gave this exercise some time and asked "what should happen if finally block contains control flow statements?" the reasonable answer should be "take precedence", because the behavior would be surprising/cursed otherwise.

Wouldn't the reasonable behaviour be "throw a compiler error"?

Re:JVM exceptions are weird: a decompiler perspective by friendzis on Monday November 10, 2025 @03:23PM (#45880471)
Can the compiler perform sufficiently deep static analysis to cover all possible cases? At least in Java it is by definition impossible.
Re:JVM exceptions are weird: a decompiler perspective by Akronymus on Monday November 10, 2025 @04:05PM (#45880918)
I don't see why not. If it's in a finally block, disallow control flow. Or am I missing something?
Re:JVM exceptions are weird: a decompiler perspective by taneq on Monday November 10, 2025 @08:08PM (#45882953)
If it's that hard to figure out what the code should do by looking at, then it's probably not a great thing to allow in the language.
Re:JVM exceptions are weird: a decompiler perspective by overfeed on Monday November 10, 2025 @11:28AM (#45877592)
> But if you gave this exercise some time and asked "what should happen...

This applies to any cursed JavaScript code too (see https://jswtf.com). The increased cognitive load required to figure out unintuitive/counterintuitive code is what makes it cursed.

Re:JVM exceptions are weird: a decompiler perspective by taneq on Monday November 10, 2025 @08:01PM (#45882897)
It's been many, many moons since I touched Java but I would have expected this to run the finally { } clause and then return from the function (similar to how in C++, objects on the stack will run their destructors after a return call before the function ends. I certainly wouldn't expect it to cancel the return.