An unavoidable part of being a programmer is that your programs are going to give a lot of errors.
You can't avoid bugs.
You can't avoid programs breaking.
A simple print function here with, let's say, a misspelt word with let's say no comma.
If I run this we've been seeing this a lot, right?
We've been seeing a lot of this read throughout this course, a lot of these errors.
If I try to add one plus, let's say.
True.
Click Run.
That works, but then all of a sudden I decide to add hello to one.
And yet again, we see that red this time a type error.
An error.
That crashes our program like this.
It's called an exception.
And Python.
Raises these exceptions whenever the interpreter says, Hey, I have no idea what you're doing, something's
wrong.
I don't know what I'm doing anymore.
I'm going to stop whatever I'm doing and I'm going to give you an output.
This is bad because right now we're just playing.
But if we're Google and people depend on us to maybe check their email, to watch YouTube videos, if
our programs have exceptions and they stop working and they error out, that's bad.
That could cost companies millions.
So how can we handle these exceptions that crash our program?
Well, we need something called error handling.
It's a natural part of being a programmer.
And a great programmer is able to handle errors in their program.
Because instead of letting our programs just air, we're able to handle them so that if we get something
like a type error like this, we can actually handle that error within our programs.
And in this section, we're going to learn all about that.
But the key takeaway is that error handling allows us to let the Python script continue running, even
if there are errors.
Sounds pretty interesting, right?
Well, before we get into that, let's just talk about some of the errors that exist in Python.
We've already seen type error where we tried to do something between two different data types that are
incompatible, such as adding int and string and these built in exceptions in Python, there's lots
of them and I'll link to this resource so you can take a look.
But if I scroll through here, you see attribute error and of file error.
We keep going, we have a name error.
We have so many error reference errors just a ton.
Let me just go through some of them that are quite common.
One is a syntax error.
If I do def and let's say.
Whoo hoo!
And I forget a semicolon here, and I run this.
I get a syntax error?
It's not a standard python syntax.
I have to fix my error.
I can also get something called a name error if let's say I use one plus name like this and I click.
Name like this.
You can see the red underline here.
I get undefined name so that if I run this code.
I'll get a name.
Her name is not defined.
We've seen type air.
But what about this?
What if we have a list?
Let's say Lee.
One, two, three.
And I access lie, let's say an index of five.
If I run this.
I get an index error list.
Index is out of range.
What if we had a dictionary?
So let's say we're creating a dictionary here.
We'll say a.
He calls one and I will just leave it at that.
So this will be a dictionary.
If I do dictionary here and just try to access something that doesn't exist and I click run.
I get a key error because I'm accessing a key that doesn't exist.
We can also have, let's say, five divided by zero, which should give me a zero division error because
I'm trying to divide by zero and well, you just can't do that.
So there's lots of errors or built in exceptions in Python.
Now, you don't have to memorize any of these.
Most of the time they'll come up in your program and you'll know what they mean, but you can always
use as a reference to read them.
But the question you should be asking yourself is How do we avoid these?
Because most programs aren't just us typing information.
Usually programs interact with the outside world, right?
When I log into my email, I let a user use their email and password.
When I play a game, I let a user control a character.
So programs are constantly interacting with the outside world, and they have to make sure that whatever
input they get is the right type that they're expecting.
Because if they receive something that they didn't expect, like number, and this number just happens
to be, let's say, zero, that we give it.
Well, this user broke our program.
So we need to handle these situations where a user might give something bad that we wouldn't want.
In the next video, we're going to explore how we can handle our errors.
I'll see you in that one.