What is use of finally block in exception handling rather Exception class can catch all the generated exceptions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is use of finally block in exception handling rather Exception class can catch all the generated exceptions?

try{ } catch(){ } finally{ }

9th Mar 2017, 1:51 AM
Aman gautam
Aman gautam - avatar
3 Answers
+ 3
finally black execute always. You can write code to do task like memory release (object dispose in file read ) Long answer: "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."
9th Mar 2017, 1:55 AM
Eranga
Eranga - avatar
+ 1
Yes it can be used to close the file and connections.
10th Mar 2017, 4:01 PM
Rajat
Rajat - avatar
0
finally block is whether the program got exception or not finally keyword will give output..very mantatory things we can place inside the finally statement
4th Jun 2017, 5:28 PM
Raju G
Raju G - avatar