4 ms·
As a random tangent, this is an "exception pattern" (Java). int foo() { int i; for (i = 0; i < 10; i++) { try {
by marginalia_nu 4y 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.
- layer8 4y agoThis 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.html#jls-14.20.2 https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.htm...
- marginalia_nu 4y agoIt is a pattern with an exception.
- layer8 4y agoPatterns are solutions or practices applicable to a certain problem. The code you presented isn't that.
- marginalia_nu 4y agoThat is one definition of the term.