Let's go back to our example of our game where we have a user log in and they have to provide their
H.
Remember, if I click run it's going to ask me, Hey, what's your H?
And if I give it something that it doesn't like, it's going to keep asking me until.
I provide a proper value.
There's one other piece to the tri except block that we haven't covered.
So we know that we have try, we have accept and then we have else.
But there's also another thing called finally and finally runs.
At the end after everything has been executed.
Let me show you.
If I do print.
Let's say, och, I'm finally done.
Let's see what happens if I click run here and I give it a proper age.
I get.
Thank you.
Because my tribe block succeeded.
Nothing has failed.
I go to the LS and I say Thank you.
And now I break out of the.
While loop, but finally says, Hey, no matter what, at the end of it all, I want you to finally
do something.
Let's see this in action.
If I do something like zero.
Again.
Please enter h higher than zero but then also still get och, I'm finally done.
What if I enter gibberish?
I get?
Hey, please enter a number.
But also.
Och, I'm finally done.
So finally runs regardless at the end of everything.
Why is this useful?
Maybe you're working on a game server and you want to make sure that you log out any activity on the
server.
So let's say a user tries to log in even if they try to enter the wrong information.
We want to log that activity so that maybe we can detect that there's people trying to break our program
or just to have our records that, hey, this user has tried to log in ten times.
So finally is very useful.
Now, let me ask you a question.
What happens if I do something like this?
Let's say I have here.
Continue.
And maybe another break statement here.
And then maybe inside of here, I'm also going to have a print.
Can you?
Hear me?
What happens if I do something like this?
Pause the video if you need to.
Otherwise, I'm going to hit, run and see what's going to happen.
What is going to happen if I enter zero?
Ready.
I get.
Please enter h higher than zero because I get a zero division error.
A catch us here and then we break out of the loop.
The finally gets run, but print never runs because, well, we break out of the loop and remember,
this is surrounded by the loop.
Let me remove this so we can see it better.
Okay.
What happens if I do something like this?
What if we run the program again and this time give it gibberish?
I get?
Please enter your number.
Okay.
I'm finally done.
And then what's your age?
So what happened here?
Well, we tried this.
We got an error, a value error.
It says, hey, please enter a number, and then it's going to continue.
Now continue instead of break doesn't break out of the loop out of the while loop.
Instead it comes back to the top.
So it's going to try again.
This part, what's your age?
So what if we finally try to log in properly and give it, Hey, we're 18.
What happens next?
There you go.
I get.
Thank you, because we've tried it.
Everything works.
So no exception.
So we are going to get.
Thank you.
And then we break out of the loop.
So once again, this print never gets printed.
Can you hear me?
But then finally.
Okay, finally is done.
If we didn't have this break statement here and I click Run.
Give it 18.
Oh, boy.
Finally, I can hear you.
This print finally gets printed because we don't break out of here.
We don't break out of here.
And then we run this line.
Hopefully that makes sense.
That was a bit of a trick question, but you're starting to get the hang of it.
I'll see you in the next video.
Bye bye.