In this lesson, I'll show you how you can generate your own exceptions
in case your code encounters unexpected situations.
So these are the four keywords are most important when it comes to error
handling and catching exceptions.
The final keyword that is related is one called raise.
And what this allows us to do is to raise our own exceptions.
For example,
let's say I decide that at the very end of all of our file,
no matter if this file went through or if it didn't, if there were an error
if there wasn't error,
I'm still going to raise an exception. And to raise an exception
all you have to do is tap into one of the known exception classes.
So let's say I just decided to raise a key error. Now, when I hit run
you can see no matter what happened, and in fact, there is no error.
I decided to raise this error and that's what I see right here.
I could change that to a type error,
and that is what will crash my code.
And I can even specify a message along with this error.
So I could say raise a type error and let's just say the message is,
and you can see now when it runs, it hits that type error. And it says,
this is an error that I made up. So when might you want to raise errors?
Well,
let's comment out all the code that we have because we have that type error at
the end so it's always going to crash the code no matter what.
So let's comment it out and let's start from scratch.
Let's say that I decided to calculate the body mass index or the BMI of
somebody's and I'm getting them to input their height as a float.
So this is going to be their height in meters and their weight as an integer.
Now I can calculate the BMI by taking the weight / height * height.
And you can either do this and wrap it inside some parentheses,
or you can use the power function or even better
you can actually use the builtin exponent, so height to the power of 2.
No matter which way we use, we can print out the BMI.
But if I provide a height that is just an unrealistic non-human height,
lets say there were 45 meters tall,
like some sort of four storey building.
And then I gave them a weight, so let's say they are 67 kilos.
Then obviously a mistake has been made somewhere in the height, right?
Because this is just not within the normal human range.
This is like Godzilla's height. If I go ahead and hit enter,
you can see that we get at BMI and it is calculated correctly
and there are no errors because everything is perfectly valid other than the
fact that this height should not really ever go over 3 meters.
In this case, we might want to raise our own exception.
So we can go ahead and say that if the height is greater than 3 meters,
well, in that case, it's probably not a valid height.
So we can go ahead and raise a value error.
So this is an error that says that whatever value was entered
as the argument is probably wrong.
And we can accompany that with a message.
Human height should not be over 3 meters.
Now, when we run our code and the user mistakenly types
a wrong height, then it's going to actually give us this error
and it won't proceed to give them an inaccurate BMI.
So this is how you might raise your own exceptions
when there are certain things that are not caught by the code because it's
perfectly valid code, but it's in fact going to generate the wrong results.
Now that we've seen all of these aspects of exceptions,
I want you to have a go at catching some exceptions that can commonly occur.
So head over to the next lesson where I've got an exercise for you.