return statement in java exception handling
如果执行不会导致异常,则控件将转到finally块。那么,try块中的返回语句是否正被JVM忽略?。或者,如果发生异常,则控件将转到catch块,同时忽略返回语句,控件将转到finally块并从finally返回。
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Helper { public int showException(int a, int b){ try{ int c=a/b; return c; } catch(Exception e){ return 0; } finally{ return 3; } } } |
因为无论你是进入
从Java文档
The finally block always executes when the try block exits. This
ensures that the finally block is executed even if an unexpected
exception occurs.
号注意:只有在
If the JVM exits while the try or catch code is being executed, then
the finally block may not execute. Likewise, if the thread executing
the try or catch code is interrupted or killed, the finally block may
not execute even though the application as a whole continues.
号
根据设计,
The finally block always executes when the try block exits. This
ensures that the finally block is executed even if an unexpected
exception occurs. But finally is useful for more than just exception
handling — it allows the programmer to avoid having cleanup code
accidentally bypassed by a return, continue, or break. Putting cleanup
code in a finally block is always a good practice, even when no
exceptions are anticipated.
号
在此处阅读更多信息
无论try catch块是否执行,finally块始终执行
无论是否调用catch块,finally都将最后被调用。也许您想阅读更多的文档。最终尝试捕获