How does return work in try, catch, finally in Java?

Yes, it’s confusing.

In Java, all program control paths of a non-void function must finish with a return, or throw an exception. That’s the rule put nice and simply.

But, in an abomination, Java allows you to put an extra return in a finally block, which overrides any previously encountered return:

try {
    return foo; // This is evaluated...
} finally {
    return bar; // ...and so is this one, and the previous `return` is discarded
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)