weblog

技術的なメモ置き場。

【Java】try-catch-finally文

try-catch-finally文の順序は変えられない。

// OK
try {} catch(Exception e) {} finally{}
// NG
try {} finally{} catch(Exception e) {}
// NG
try {} finally {} finally {}

catchブロックとfinallyブロックにreturnがあった場合どうなるか?

try {
    // omit...
} catch (Exception e) {
    return 1;
} finally {
    return 2;
}

この場合、2が返る。