Finally, always executed
Your code is always executed completely, regardless of whether an exception has been thrown. So your code should be:
try { i = 1; } catch { i = 2; } finally { i = 3; } return i;
But in this trivial case, finally, the block will not make much sense. Because we will always return 3 no matter what happened before.
Finally, used to free resources
Block
finally should usually be used when you need to free some system resources allocated in a try block (i.e. open a database connection with read data in a try block and close it in finally )). Therefore, they will always be released regardless of whether there was an exception or not. In this case, it makes no sense to use a finally block.
source share