Reputation: 213
So I know that a finally
block of code will execute regardless of an exception, but is it bad to not use it? I've been using just try/catch
and wanted to know if this was a bad practice. Or does it not really matter?
Upvotes: 3
Views: 4069
Reputation: 25351
The answers by Subdivision and Jon are great, but I just want to complement them with a clarification about a common misconception of how the finally
block works, mainly the answer to this common question:
What is the difference between placing code in the finally
block vs. leaving it right after the catch
block?
Incorrect Answer: the code after the catch
block will not execute in case of an exception, unless you put it in the finally
block.
If that is incorrect, why do we need finally
?
Well, first of all, the code after the catch
block will run as long as you handle all exceptions appropriately, which means that no exception should escape the try..catch
unhandled. finally
on the other hand, will run almost always even in the case of an unhandled exception escaping the try..catch
. I said "almost", because some unhandled exceptions may skip the finally
block, but that's rare.
Another difference is: if you exit your code in the try
or catch
blocks, the code after the catch
block will not run. We may think that this is obvious, but it can be tricky sometimes. Examples: return
, break
and continue
(if your try
and the code after are in a loop), throw
(if you use it in the catch
to re-throw an exception to the caller). finally
will still execute in all those conditions.
One last thing to consider: The use of finally
clearly states your intention, so it contributes to a more robust and self-explanatory code. If you handle all the above-mentioned conditions carefully and write code after the catch
thinking that it is as guaranteed to run as the finally
block, you will probably be fine.... Well, until another developer needs to modify your code in the future and decides to add a return
(for instance) in the try
.
Conclusions: If you have code that you need to guarantee its execution after your try..catch
, it is always recommended to use finally
.
Upvotes: 4
Reputation: 78
When you have code that could generate a mistake, you put this code in a try/catch
. And when we add finally
is to run code not dependent on the error captured above.
PS: the lines of code contained in 'finally'
are always executed.
Upvotes: 0
Reputation: 479
Short answer, no. It all depends on what happens in your try block. I would say that most of your try-catches will probably not need finally. Finally is however required when you are opening resources in the try block such as files, streams, network etc that you have to close (whether an exception is thrown or not)
Upvotes: 6
Reputation: 1500873
If you don't have anything you need to clean up at the end, that's perfectly fine. finally
blocks are almost always about resource cleanup... and there are plenty of times where you want to catch an exception but don't have any resources to clean up.
Having said that, only catch exceptions when you can actually handle them or want to wrap them in a more suitable type. My experience is that catch blocks should be far rarer than try/finally or try-with-resources blocks. If you find yourself catching exceptions in most methods, that probably indicates you're misusing them.
Upvotes: 9