Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
korrat5865yNo, not really. A finally clause is always executed, even if an exception is not handled in a catch clause. The code after the try would not be executed in that case. -
asgs109125yIf try or catch block throws exception, then it is not equivalent to the code appearing after the try block
Think of it like a cleanup block to tear down anything that might have been tried -
Save for a hard crash, forcibly exiting the application, etc. you have an iron clad guarantee that the finally block in question will *always* run (even if you return from the method in the try block, or an unhandled exception is thrown in the try block.
-
Others explained it but for uses I can think of
- releasing a mutex lock or similar
- properly closing file streams or network sockets
- making a backup save of the users data so it won't be lost in case of a full crash
Related Rants
-
trogus64
New devRant web app for desktop is now live! (https://devrant.com - the .com will now redirect to feed if you ... -
c3rberus11599% of the devs be like catch(Exception e) { // Not a single fuck given } -
SMtengwana4
Was going through some old gadgets n found this fossil.
Give me the fucken code Blackberry and I'll catch it ...

What's the point of the "finally" block? Isn't it functionally equivalent to everything after the try { } clause?
question
try
finally
catch