finally in exception handling

2019-02-23 02:52发布

What exactly does a finally block in exception handling perform?

8条回答
迷人小祖宗
2楼-- · 2019-02-23 03:49

finally block is mainly used to perform close statement for example con.close that is to close connection from database....try block is always followed by either catch block or finally (or both also)...If you once entered in the try block then your finally block will be definately execute except system error,exception in finally block....Main key point of finally block is that it always be executed even the exception is handled or not..

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-23 03:50

I use it a lot for cleaning up open resources when there are multiple return statements in a block of code, making the code a lot cleaner as you don't need to clone the same 'close resource' code before every return statement. It's guaranteed that the code will call the finally section, even if you do a return within the try section. It also helps with code safety in this instance, since the programmer could easily leave it out by accident.

查看更多
登录 后发表回答