参见英文答案 >
Behaviour of return statement in catch and finally6个
我运行这段代码:
我运行这段代码:
public static void main(String[] args) { System.out.println(catcher()); } private static int catcher() { try { System.out.println("TRY"); thrower(); return 1; } catch (Exception e) { System.out.println("CATCH"); return 2; } finally { System.out.println("FINALLY"); return 3; } } private static void thrower() { throw new RuntimeException(); }
我希望在输出中看到这个:
TRY CATCH FINALLY 2
但令人惊讶的是输出是:
TRY CATCH FINALLY 3
我糊涂了.哪里返回2声明?最后回归是一种糟糕的做法吗?