Hey everybody welcome to the next video.
So in this video we're gonna be going over air handling.
So this is just as important as working with configuration files and logging.
Now there's a few different aspects of air handling that you're going to want to deal with.
You're going to either want to do an area where you cease function such as something detrimental happened
and you want to stop the application right now.
There's a few other instances where maybe you just want to change you're expecting a certain error and
that changes maybe you need to make a certain assumption and you'll do some other logic instead.
That's not a problem.
Let's say it's you're taking in a variable and you're checking the type if you're expecting a string.
You want to do one thing if you're expecting an integer you're doing another.
Maybe you do a quick comparison check and then if it errors out you have your answer.
That's another way.
There's another option where maybe you expect a certain error to happen occasionally but it's not detrimental
to the application process flow if that's the case you just want to suppress it and then continue on
with its life.
You don't want to stop it anyway.
So in Python there's a few different ways we can do this.
So this is normally done with a try except So what you do is you try with a try and you do some sort
of data.
So in my case the I.T. automatically fills it.
So you try perform some sort of logic or code.
This can be as big as small as you want and then accept and then the type of exception.
So in this particular case it's using a broad exception.
And what's important to understand with python is that any other type of exceptions such as a key error
is all based.
They're all inherited classes by the main exception class.
So at the end of the main exception class you have an as e..
Now this part is not required.
You could just do accept however as e basically allows it stores the error that occurred in the E variable
then you can rise or raise an E which essentially causes the application to stop or to kick back to
its calling function in this particular case its main so it would stop the function and then spit out
the error which is common on what you'll see.
So if we pull up here you can see in this case we have our main track and we've got our issue where
we didn't pass three up two arguments we we're passing more arguments than one in our person from a
different video.
So you can see that this error message is what will be stored inside of this raise.
Now what we're going to do is work with us a little bit.
So let's say we want to create a temp dictionary
and we'll set this to do name.
So what we're going to do is we're going to do result sequel to dict and then we're going to give it
something that doesn't exist does not exist.
So what's gonna happen when we run this is it's attempting to access something that doesn't exist.
And we're gonna get our error
Nina tab there.
Make sure it's under Maine.
So go ahead and run this again.
We can see we get this trace Becker.
This is does not exist.
However if we don't write if we don't raise that we just do pass which is essentially do nothing.
It just skips right past it nothing happens.
So we've suppressed the error.
So this is how you would suppress an error if we needed to do something.
But however let's say if we don't do at the bottom here we wanted to print out
result.
Right.
So here's what's going to happen.
We're going to get our pass but we're gonna get an error saying result did not occur.
However let's set let's say here is all
tempt number in new name.
Right.
So if this doesn't exist will default to name.
So this is a way for us to change the logic that we're using.
As you can see no errors even though an error technically occurred.
We've caught the error and we've done some other logic with it.
Then we pointed out the result.
Now you can still use it.
So you can still do print you know did not find original value and we'll do zero and then just pass
any.
Right.
So this gives us a way to show that basket for value doesn't does not exist.
So that gives us our base part of the error message as well as we continued on.
So it's important to understand how to handle your error messages in certain ways.
Now in this particular case the does not exist is a local unbound local.
So this is the type of error.
Right.
So if we come in here and we changes from exception to just the unbound local error since it's an inherited
of that nothing changes I'm sorry it is not an unbound local error.
That's because we are looking at result technically us looking for that value is a key error.
So we do care here area about that.
So the types of error messages you get normally with a python error message still looks something like
this is going to stay.
Trace back.
Show this.
And then the type of error.
And then the result of the error.
So again you can look if you find yourself running into code where you're running into a problem or
you know you're going to foresee a certain amount a certain problem and you plan on doing something
with that.
It's important to catch it and do with it what you need whether it's airing out stopping the application
but it's also important to understand is that when you're handling errors you do not want to suppress
everything because then something really does go wrong that you weren't foreseeing in some way shape
or form then your application is going to look like it completes correctly.
And if you have logging on the log files as an example you have something like Splunk that's going to
alert when an error occurs and you're going to run into a problem where you think something's working
well it's not.
So it's important to be very particular when you're catching errors to not use exception as much unless
there's a specific need to or you're printing the actual error message on the screen or logging it.
If you're doing that and catching and everything is perfectly fine as long as you're logging it but
do not suppress under the exception of everything.