This video was brought to you by Indently .
Io , learning Python made simple .
How's it going guys ?
In today's video , we're going to be talking about the finally keyword because a couple of days ago , I received this comment that finally is the most useless thing , I swear to God .
And it's not the first time I see this kind of comment .
A lot of beginners have no idea what finally actually does .
Anyway , let's get started by creating an example .
So right now , we're going to fill out this information with some code that raises an error and we're going to manually raise a value error that says that this was a bad value .
And then we're going to catch that .
So 'accept value error' .
And here what we're going to do is print that we are handling this value error .
So 'handling value error' .
And in the block , we're going to print that finally is being executed .
And with this code , we're going to encounter the finally is always going to execute no matter what happens , unless , of course , your computer turns off .
Then it probably won't have the time to actually execute the finally block .
But let's run this and see what happens .
As soon as we run this code , what we're going to get back is that we are handling that value error and that finally is being executed , just as we expected .
But at this point , you might be saying , okay , we can just remove the finally it and then we're going to print that finally is being executed .
Or in this case , there's no finally but it's going to be executed no matter what .
And if we were to run that , we will get the exact same output .
So in this very specific scenario , you are correct .
But now watch what happens if we raise another error inside the accept block , which can easily happen when you're trying to handle an error .
And here , we're just going to raise a custom exception and say we did not expect this .
And now the next time we run this , we're obviously going to have an exception , and our finally print statement will not get executed because that code is unreachable .
As soon as an exception occurs in our try and accept block , it exits out of the program before it can execute this line of code .
But now let's reintroduce the finally block to our code .
And this time , what you should notice is that finally will be executed no matter what happens .
Here , you might not see it because it happened so fast , so you're going to have to scroll up .
But you will notice that finally did get executed .
Executed .
So once again , finally will execute this code no matter what happens in our try and accept blocks .
And usually , you'll see see this being used when you are working with files .
Once you open a file and start performing some operation on it , you're going to want to make sure that if anything unexpected happens , that you can close that file properly .
And that can help is so important .
But there is one thing I learned recently that's quite interesting and that is that even if you were to exit out of your program , so let's say instead of raising an exception , we were to print system dot exit and here we can type in terminating program as the exit code , finally will still be executed even if we exit out of the program .
So really , there's no stopping finally .
And again , don't mind the order of these .
They're just happening all at the same time .
So printing them out might appear in a random order .
I mean , as you can see , if I run this several times , we will continuously get these in random orders .
And with all this being kept in mind , there are a few things you need to note .
And I recently learned this from an m coding video .
I honestly never even thought about doing it until I watched this video .
But suppose you have some sort of function and we're just going to call this function that returns some sort of integer and we're going to paste all of this inside here .
Now inside the accept block , we're going to return 1 if it raises an exception .
And in the finally block , we're going to return 0 .
And if we were to consider the mechanics of finally , what we should expect as a return is 0 , no matter what happens in accept .
Now we can verify that by printing the function .
Now When we run this , we will get 0 as a return , even if it executes all the code inside the accept block .
And once again , that's because finally will always run no matter what happens in the try and accept .
Even if we tell the program to return 1 , we are forcing it to return 0 because we included a finally block .
And in other words , it's just overwriting the other return .
And you might be asking , well , this should raise a syntax error .
Why don't they raise a syntax error for this kind of code ?
Well , someone did propose that as an official change to Python but it was rejected because this is something that the developer has to know .
And instead of restricting their freedoms , pep decided to leave it as is .
And I also want to mention that , in general , it's a bad idea to also include things such as continue or break .
Anything that has to do with the flow of the program in the finally block can be considered a bad idea because it will overwrite anything you do in the accept block .
But I did find it quite silly that you could create a while true loop and create a try and accept block , which let's say raises an exception .
I am not going to say anything specific other than that this is a bad idea and we're going to accept that exception by breaking out of this loop .
So here we're going to print breaking and we will break .
So we can finally end this loop .
But funny enough , you can include a finally block and type in continue .
And as you might have guessed it , the finally block will completely ignore the accept block .
So what we're going to get is an infinite loop .
So there are a lot of silly things you can do with try , accept , and finally .
But the moral of the story is that finally is incredibly important to our programs because it guarantees the execution of code inside a try and accept block .
And excluding it really guarantees you nothing .
I mean , you can put continue down here but it's only going to print breaking once because it won't be able to reach the continue block or not the block but line 11 , which is located below the try and accept block .
Anyway , I hope that cleared up why finally is so important in Python or at least the importance of finally in Python and I would love to hear what you think about finally in the comment section down below .
Otherwise , with all that being said , as always , thanks for watching and I will see you in the next video .