Hacker News new | ask | show | jobs
by marginalia_nu 1279 days ago
As a random tangent, this is an "exception pattern" (Java).

    int foo() {
        int i;
        
        for (i = 0; i < 10; i++) {
            try {
                throw new RuntimeException();
            }
            finally {
                if (i < 5) {
                    continue;
                }
                else {
                    break;
                }
            }
        }
        
        return i;
    }
Compiles. Runs. Returns 5.
1 comments

This is not an “exception pattern”. It’s just how the language works, and is a consequence of the concept of “abrupt completion”. It means that you can unconditionally (i.e. independent from whether there is an exception or not) divert the control flow using finally. It’s not much different from how when an exception is thrown from the finally block, the prior exception gets lost.

From the language spec [0]:

If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).

[0] https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.htm...

It is a pattern with an exception.
Patterns are solutions or practices applicable to a certain problem. The code you presented isn't that.
That is one definition of the term.