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 error 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 H?
And then here, all we're going to do is just print the H.
Nice and simple program.
If I run this.
And I type in, let's say, ten.
There you go.
Everything is working.
But what if I didn't enter a number and I go like this?
Well, that's 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 off, 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 int, 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 literal 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 hoops, we have the tri keyword which says, Hey, I want you to try this piece of code
and we have to make sure we indent it.
So we try this piece of code.
Now anything that happens inside of the tribe block.
I'm able to handle.
See where handling is coming from.
AYRES With the except keyword.
And except.
Says, Hey.
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 paste,
add 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 accept 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 click run again and ask for the H once again.
So how can we make sure that we keep running this until, well, the user actually enters something
valid?
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 what's my H let's say.
Nope.
Please enter your number.
What's your age?
Let's try to break it again.
Please enter a number.
All right, fine.
My age is ten.
There you go.
I get the name ten, but.
Oh, it's still asking 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 an 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 error, do this.
However, if there is no error 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 accept ls block can go anywhere.
We can wrap our entire file in a try except else 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 errors.
But let me ask you something.
What if I did instead of print h 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 our 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 accept 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 lock only accepts value.
Errors.
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 h higher than zero.
So that if I run this now.
And I do zero I get please enter h higher than zero.
If I do some gibberish.
Whoops.
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 accept block is going to run only once.
As soon as it catches the error it's looking for, it's going to run.
And then come back to the loop.
So only one of the errors will be caught.
Let's take a break and learn more about error handling in the next video.
Bye bye.