Instructor: Let's try a different example here.
Let's say I'm creating a simple sum function.
Yes I know it already exists in Python
but we'll just create our own.
A simple one that takes num1 and num2
and in here we'll just return num1 plus num2.
Now if we were to just simply do sum1
and sum2 and I ran our program.
Well let's print it out first.
I click run.
I get one and two
because it just adds the two strings together.
So let's just do this
and click run.
I get a type error.
Must be a string not INT.
So based on what we learned
how do we handle this?
Well we can add a try block in here and wrap this function
or we can build it directly into our function.
So I can say try.
This part and here have an except clause
that says perhaps if we get a
any type of error we'll print something is wrong.
If I run this.
I get something is wrong.
But here's a problem.
By just doing except with no exceptions.
As a programmer, I'm reading this
and I don't really know what actually went wrong.
Because it could be so many things.
So a good practice is to always catch these errors based
on a specific exception.
This way you know what the error is
and you can be more descriptive.
So here we can say type error.
And this type error we can just say please enter numbers
so that if I run this I get please enter numbers.
So now, alright, my error is more descriptive.
I should fix this and make sure that I enter numbers.
Awesome.
Now a common pattern when doing error handling
is to do something like this.
Type error as
and then doing something like a variable.
So whatever variable we want.
In our case, let's just say error.
So I'm saying, hey, if you catch type error
let us use error in our error message.
So I can say here
plus error.
So that if I run and let's do a string here.
If I run.
Look at that.
I get the error printed.
Let me just open this up a little bit
so you can see it clear.
Look at that.
It actually gives me the error that I want.
So that I can see that on line five.
On this line I get a type error.
Unsupported operant.
And I get a descriptive error of what is happening.
So maybe I could use it in my output message.
But what if I don't want to use this?
And I do run?
All right, everything is working.
Again, if I do plus error and I click run.
And this is actually tricky
but it says here during handling of the above exception,
which I'm doing, another exception occurred.
Oh boy.
I try to handle something
and I had an error within that error.
So this is sometimes tricky
but what it means is what we get
out here is actually an error object.
It's a built in exception in Python.
You can't add in here the way I have.
Instead we wanna use an F string.
So I do F and then here pass it the error.
So that now
if I run this
I get please enter number.
Unsupported operant type S for plus INT and STR.
So I actually get this error available to me.
Now if I was to just print this error
let's see what it looks like.
If I click run.
You see this is the part of the error that we get.
So it's very, very useful
if you wanna give meaningful errors to your users.
Another interesting thing you can do
is something like this where you wrap these errors together.
So let's say we have zero division error as well.
And here we'll just say, oops.
If we, for example, divide num1 by num2.
Let's just run this first.
I get, oops.
If I do one divided by zero
and I run this, I also get, oops.
So I can handle multiple errors the same way.
I can also say as error here
and just actually print out the error.
And if I run this I'll get division by zero.
Or if I do a string
and run this I get the other error type.
So this, again, is really useful to combine different errors
if you wanna handle them the same way.
But what if we actually wanna raise our own errors?
That is we wanna throw our own errors.
Maybe inside of this except block I
actually want the error to display
for certain types of errors.
How could we do that?
Let's find out in the next video.