try-catch-finally
Why Finally?
A Finally block will be executed after a try block if no exception has been thrown or after a catch if an exception was thrown. This means that a finally block can be used as 'clean up' - a place to close files or connections etc, whether an exception is thrown or not.
If a try block throws an exception and the catch block propagates the exception (throws it again), the finally clause will still execute. If the finally clause executes a return statement, it overides a thrown exception (so the exception will not be thrown; instead the return will occur).
1。finally块在以下情况下执行:
try块执行完毕且没有抛出异常;
try块执行,抛出异常,catch块自己处理该异常后;
try块执行,抛出异常,catch块将该异常向上抛出;(如果fanally块中有返回的声明,那么该异常将不被抛出,相反,将该声明返回)
注意:无论try块,还是catch块中是否有语句 return; fanally块都会在return到主程序之前,执行。


评论