Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of finally in a try catch/except finally statement

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?

like image 399
Ali Avatar asked Mar 13 '12 16:03

Ali


People also ask

What's the point of finally in try catch?

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.

What does finally do in TRY except?

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.

What is the point of finally in python?

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.

What is the significance of finally?

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.

What is the difference between try catch catch and finally block?

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.

What is try-catch and TRY-CATCH-FINALLY exception?

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.

What is the difference between the catch and try statements in Java?

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.

When to use try catch in C++?

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.


1 Answers

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:

  1. If code within the try block exits via return
  2. If code within a catch block either rethrows the caught exception, or--accidentally or intentionally--ends up throwing a new one.
  3. If the code within the 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).

like image 197
supercat Avatar answered Oct 29 '22 22:10

supercat