I have used try-catch/except-finally variants in many languages for years, today someone asked me what is the point of finally and I couldn't answer.
Basically why would you put a statement in finally instead of just putting it after the whole try-catch block? Or in other words is there a difference between the following blocks of code:
try{ //a} catch {//b} finally {//c} try{//a} catch{//b} //c
EDIT:
PEOPLE, I know what finally does, I have been using it for ages, but my question is in the above example putting //c
in finally seems redundant, doesn't it?
The finally block will execute when the try/catch block leaves the execution, no matter what condition cause it. It always executes whether the try block terminates normally or terminates due to an exception. The main purpose of finally block is to release the system resources.
The finally keyword is used in try... except blocks. It defines a block of code to run when the try... except...else block is final.
Important Points – finally block is always executed after leaving the try statement. In case if some exception was not handled by except block, it is re-raised after execution of finally block. finally block is used to deallocate the system resources.
The finally statement lets you execute code, after try and catch, regardless of the result. The following example would give result from finally block even though catch block got the error.
The code that will possibly throw an exception is enclosed in the try block and catch provides the handler for the exception. The finally block executes the code enclosed in it regardless of whether the exception is thrown or not.
Control flow in try-catch OR try-catch-finally Exception occurs in try block and handled in catch block: If a statement in try block raised an exception, then the rest of the try block doesn’t execute and control passes to the corresponding catch block.
The technical term for this is: Java will throw an exception (throw an error). The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
The keyword catch should always be used with a try. Sometimes we have an important code in our program that needs to be executed irrespective of whether or not the exception is thrown. This code is placed in a special block starting with the “Finally” keyword. The Finally block follows the Try-catch block.
The purpose of a finally
block is to ensure that code gets run in three circumstances which would not very cleanly be handled using "catch" blocks alone:
try
block exits via return
catch
block either rethrows the caught exception, or--accidentally or intentionally--ends up throwing a new one. try
block encounters an exception which for which the try
has no catch
. One could copy the finally
code before every return
or throw, and wrap catch
blocks within their own try/catch to allow for the possibility of an accidental exception occurring, but it's far easier to forgo all that and simply use a finally
block.
BTW, one thing I wish language designers would include would be an exception
argument to the finally
block, to deal with the case where one needs to clean up after an exception but still wants it to percolate up the call stack (e.g. one could wrap the code for a constructor in such a construct, and Dispose
the object under construction if the constructor was going to exit with an exception).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With