In Python, when your code runs into an error, it does this
thing called raising an exception,
for the most part. In other languages, this is also called
throwing an exception.
Now, in Python, we have this little 'try-except' block. So
we could 'try' some code, and then we could 'except' an error.
So if our code fails for whatever reason, we can 'except' the
error and gracefully handle it.
Now, a good example of this is '1/0', you get
a 'ZeroDivisionError'.
Simply put, in mathematics, 1 cannot be divided by 0.
And so if we do this, 'print("HELLLOOOOO")',
notice that our 'print' statement does not show up.
It caught an error on here and died.
Now, like we see our 'print' statement in the error itself,
but we don't actually see it printed out to the page.
And so this is a really good reason why we want to catch
errors and then gracefully handle them so that our program
doesn't totally bug out and die, because that's a terrible
user experience, and the first person they're going to blame
is the programmer.
So why is it important to be able to catch this particular
error or any sort of error that happens?
Well, (a) I don't want you to get blamed for anything, but (b) in
Python, when it runs into an error it usually just dies. And
see, errors are going to happen.
There's just no way around it. If it has to do with working with humans,
so maybe you're making a website, or maybe you're working
with data, or you have something that has to do in relation
to a human in any way, shape or form,
you're going to get errors,
and that's because humans come with user input, and user input,
always, always, always is a string, and users, a lot of us,
including myself, can be quite malicious.
We try to break things just because we know we can.
And so it's your job as a programmer to say, "Hey, yep, human,
you are allowed to try to break this, but I'm going to catch
that problem and I'm going to work with it anyways".
So let's go ahead and show another error here.
So let's say we've got a dictionary, and it's got a key in
here 'name', and the value is going to be "Kalob".
And then we try to do something like 'print(d[])',
and then my 'hobby', which does not exist, this key in the
dictionary simply does not exist.
We're going to see a 'KeyError'.
This one's got a 'ZeroDivisionError', and this one is a 'KeyError'.
And there's all different types of errors.
So when it comes to catching errors, there are a few keywords
we want to keep our eyes out for. 'try' is one of them, 'except'
is another one, and 'finally' is the last one.
Now, what this is going to do, is it's going to 'try' to execute the code,
if it runs into an error,
if it runs into some sort of exception, it's going to exit
the 'try' block immediately, and it's going to try to handle
the error, and then 'finally' will always run no matter what.
So let's go ahead and give this a quick little demo.
Let's 'try: total = 10/0', and we're
going to 'except' that there could possibly be an error in
there, a 'ZeroDivisionError',
so we're going to 'except: ZeroDivisionError',
and if that happens, we're going to say the 'total = 'N/A'',
it's not applicable, just can't be done,
and 'finally:' we will say 'print(
"The total is", total)',
[no audio]
and it says, "The total is N/A".
So it tried this, failed, and then went over here and said,
"Okay, we'll set the variable.
total = 'N/A'",
and then finally it ran some code.
Now, if you're doing something like this, technically, you
don't really need the 'finally' in there. You can run this,
and it works just as fine.
But I'm going to undo that, because that is a good little
example. Let's try another one.
Let's say we've got 'number1 = 50', and
'number2' is going to be user input.
So 'input("Enter a number:")', any number, and then we're going
to print the total.
So the 'total' is going to be 'number1 + number2',
[no audio]
and 'print(total)'.
"Enter a number", 123, and we get a different type of error.
We get a 'TypeError'.
Now, the reason for that is because 'number1', this is an
int, 'number2', this is a string.
Well, how are you going to add numbers to letters?
That's really hard to do.
Now, even though I entered a proper number, again a program
does not know that this came back as a string.
And so Python said, "You are trying to add letters to a number,
simply cannot do that".
But what we can do instead, is we can 'try' to do this, and
then we can 'except' that there is some sort of error in here,
and we can say 'print("There was an error")'.
And let's go ahead and move that 'print(total)' up here, and rerun this.
"Enter a number: 456, and it just says, "There was an error".
And so now our code isn't complaining.
It's not doing this.
It's actually saying, "Hey, this doesn't work".
The total was not printed, but the error was printed.
And that means, 'print(
"The program can continue without problems")'.
And sure enough, "The program can continue without problems".
Now, in our first example, that 'print' statement no longer
worked. It died right here.
Python just said, "I can't figure this out",
and so this was never executed. Down here
Python said, "Hey, I can't figure this out",
it broke out of this
'try' block, did not execute this, and executed the 'except' code
instead. Now, we've actually seen a lot of different error
types throughout this course, and I think we've seen three
of them in this particular lesson.
So in the next lesson, I'm going to teach you how to discover
what the error types are and how you can handle them.