Instructor: Error 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 in Python.
Let's say we wanted to create a program
that allows us to take a users input.
Let's say that we ask the user for an age.
So, we use the input prompt and say, what is your age?
And then here,
what we're going to do is just print the age.
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 or not,
whether they are 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,
uh-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 try.
Oops.
We have the try keyword which says,
hey, I want you 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 try block,
I'm able to handle.
See, where handling is coming from errors
with the except keyword.
And except says,
hey, if whatever's in here in the try block,
if anything errors out, I want you to say something.
So instead of having this red error
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 it my entire file here
put it inside of a try block.
And what's going to happen
is instead of my program erroring 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,
well, catch it in here with the except block
and I can hear and do whatever I want.
In my case, I can say, hey, please enter a 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 age once again.
So, how can we make sure that we keep running this
until the user actually enters something valid?
Well, we've seen this before.
We can do a while loop and say, while true.
Run this.
And if this works,
well, we're gonna, hopefully, get the age.
Let's see that.
If I click run, what's my age?
Let's say...
Nope, please enter a number.
What's your age?
Let's try to break it again.
Nope, please enter a number.
All right, fine.
My age is 10.
There you go. I get the name 10, but...
Uh-oh, it's still asking me what's my age.
Ideally, I wanna 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 try, except, else 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 age,
I did let's say, 10 divided by the age.
If I run my program
and I type in here zero,
and I enter,
uh-oh, I get an error, 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 wanna handle.
And remember, these are built-in errors in here
that I can just copy and paste.
So in my case, I wanna say this is for a value error.
So that if I run my program
and I type in zero,
uh-oh, I get my error.
This except block 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 age higher than zero.
So that if I run this now and I do zero,
I get, please enter age 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 error it's looking for,
it's going to run and then come back to the while loop.
So only one of the errors will be caught.
Okay, let's take a break
and learn more about error handling in the next video.
Bye-bye.