-: 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 misspelled 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 red 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 is 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 gonna 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 are trying 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,
end of file error.
We keep going we have a name error.
We have so many 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
hoo 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 error.
Name is not defined.
We've seen type error.
But what about this?
What if we have a list,
let's say LI
1, 2, 3
and I access LI,
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
equals one,
and you know we'll 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 this 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 gonna explore how we can handle our errors.
I'll see you in that one.
Bye bye.