Air handling allows us to let the script continue running, even if there is an error.
Let's try an example to show you how we can do air handling and python.
Let's say we wanted to create a program that allows us to take a user's input.
Let's say that we ask the user for an H.
So we use the input prompt and say, what is your age?
And then here or we're going to do is just print the nice and simple program if I run this.
And I type in, let's say, 10.
There you go.
Everything is working, but what if I didn't enter a number and I go like this?
Well, that still works, but is that what I wanted to do with my program, maybe I wanted to enter
the age and calculate if they can play the game of not or not, whether they're 18 and over, how can
we fix this?
Well, one thing we can do is just to wrap this in an ENT, right, so we convert whatever the user
gives us into an integer so that if I run this.
And I type this.
Oh, I get an error, a value error invalid litoral for int, I'm trying to convert.
This into an integer, but it's telling me that's an error, I can't do that.
So how can we fix this with error handling in Python?
We can do it this way.
We have the tri loops, we have the tri keyboard, which says, hey, I want you to try this piece of
code and we have to make sure we invented.
So we try this piece of code.
Now, anything that happens inside of the tri block.
I'm able to handle.
See where handling is coming from, areas with the accept keyword.
If whatever's in here in the tri block, if anything airs out, I want you to say something.
So instead of having this red air that exits out of the program, do something else.
Do this.
So I'm going to say.
Print, please enter a number.
Let's see what happens now.
If I click, run.
What is my age?
I'm going to just write gibberish.
And look at that, I get please enter a number.
I am handling my errors here by wrapping all of my code in a try block so I could have just copy Basehead
my entire file here, put it inside of a try block.
And what's going to happen is instead of my program airing out before my program crashes, it's going
to run this code and then it's going to say, hey, if within this block anything happens, we'll catch
it in here with the except block.
And I can hear.
Do whatever I want in my case, I can say, hey, please enter our number, but we have our problem
here, right?
The program just ended and we want this to keep running.
I have to run again and ask for the H once again.
So how can you make sure that we keep running this until while that user actually enter something fairly?
Well, we've seen this before, we can do a while loop and say while true.
Ron, this.
And if this works.
Well, we're going to.
Hopefully get the H, let's see that if I click run was my age, let's say.
No, please enter a no what's your age?
Let's try to break it again.
No, please enter our number.
All right, fine.
My age is 10.
There you go, I get the name 10, but, oh, it's still ask me what's my age.
Ideally, I want to break out of this true statement, right, so how can I do that?
Well, I can use what we call in here and else block, which we've seen before.
But this works with a try except block.
We put it at the end.
It says, hey, try this.
If there's an ne'er do this.
However, if there is nowhere else, do this.
So in our case, we can just say break.
And you know what, we can even print thank you, just so we're nice if I stop this and run again.
And I give them my age.
There you go.
We're done, we broke out of the loop and everything is good.
And this tri except else block can go anywhere, we can wrap our entire file in a try except block,
we can just add it into our functions inside of our each individual functions.
Whatever we want, we can use.
This is just another tool that Python gives us to handle Eres.
But let me ask you something.
What if I did instead of printing, which I did, let's say ten divided by the age?
If I run my program.
And I type in here zero and I enter.
Oh, I get in there, please enter a number, but I did enter a number.
This is a buggy program, although I'm typing a number, it's still giving me the same error message.
So how do we handle this?
How do we handle different errors, one where it is a value error and another one that is obviously
a zero division error?
Well, in the except block, I can simply give it what type of error I want to handle and remember,
these are built in errors in here that I can just copy and paste.
So in my case, I want to say this is for a value error so that if I run my program.
And I type in zero.
Oh, I get my air, this except block only accepts valuers, any other errors it does not care about.
Hmm.
So what can we do here?
Well, one option is to copy here and say, hey, let's add another, except and in this case, I want
a zero division error in case a user enters zero, you can say please enter age higher than zero.
So that if I run this now.
And I do zero again, please enter higher than zero if I do some gibberish, oops, if I do some gibberish
I get please enter a number if I finally enter a number.
Look at that.
Thank you.
Everything is working.
Now, here's my question, what happens if we enter?
Another value error here and this value error is just going to say exclamation marks.
What happens now?
I get please enter a number, because this except block is going to run only once, as soon as it catches
the air, it's looking for it's going to run.
And then come back to the wire loop.
So only one of the heirs will be caught.
OK, let's take a break and learn more about air handling in the next video.
Bye.