public class FinallyExample {
public static void main( String[] args ) {
System.out.println( test(true) );
System.out.println( test(false) );
}
public static String test(boolean e) {
try {
if (e) throw new RuntimeException();
return "A";
} catch ( Exception ex ) {
return "B";
} finally {
return "C";
}
}
}
Bez ohledu na to, jestli v metodě dojde či ne k vyjímce, hodnota ve finally bloku nakonec vyhrává - program tedy vypíše dvakrát C C.
Inu, ne nadarmo je v idei automaticky zapnutá inspekce, která ihned varuje.
This inspection reports any instances of return statements inside of finally blocks. While occasionally intended, such return statements may mask exceptions thrown, and tremendously complicate debugging.