Difference between ending with finally:
try {
// algo
} catch(Exception e) {
// algo
} finally {
// algo
}
And don't end with finally:
try {
// algo
} catch(Exception e) {
// algo
}
// algo (no finally)
I had read that finally
it is always executed even if it is an 'exception' or simply when there are no errors, but wouldn't it be the same without the " finally
"? What changes?...
As the documentation says :
what comes to say:
So, here we can see the main difference. If we don't use the block
finally
, if an unexpected exception occurs, the part inside the block will be executedcatch
but only that. In this way, you ensure that there will also be other code that will always be executed even if an exception occurs. Also, if an unexpected exception occurs when executing the blockcatch
, the code in the blockfinally
will be executed.Exception
However, as the documentation indicates, there are some cases where the finally block might not execute:
what translated would be: