Welcome back.
So in the previous video,
we talked about handling errors and also the
different types of specific except file related errors
that we should be working with.
However,
what if we wanted to check for multiple types of errors?
Maybe in addition to the file, not be not been found.
It's possible that the file was found,
but then maybe the permissions for the file are not correct
or maybe the directory wasn't correct, you know, something like that, right?
So we want to create a program, a block of code
that can handle multiple types of errors
and also print out different error messages for each error
as it is right now, I have one error which is the file,
not found error and it's gonna print a file, not found.
But what if I were to include
the permission error as well?
It's very, very straightforward.
All I would need to do is just simply open up another block and then just say accept
and then permission
error
at my colon.
And now I can print
in brackets
and then say
sorry,
you do not have the permission
to access this file.
And that's all I got to do,
let's add one more error. So I can say accept
and then
I can say
OS
arrow,
OK. Maybe there was a problem with the operating system
and then again my colon
and then I can say print
and then in brackets,
there is an OS
OK?
And now what if I wanted to add one more block of
code that will handle any other potential type of error out there?
All I would need to do
is to again say accept.
And now
the key was I'm going to say Exception
as
E.
So basically this line will handle any other
type of error that could potentially occur.
And then I'll just simply say, put out a general message and say print
and then I'll just say, and
unexpected
error has occurred.
And now we want to stress the E here, this is going to represent the arrow.
So what I'm going to do right now is I'm going to use our F string as usual
and then
curly braces, I'm going to add the E right there.
So the last line in here will handle any other type of error.
And
the system would say
an unexpected error has occurred and then it will specify what type of error
because of the E variable in here that we created over here. And
that's it. I'm going to run my program and of course, you can see
it's gonna work perfectly fine
and this is how
you can create multiple except blocks
that will handle multiple different types of potential errors that might occur.
Thank you for watching. I will see you in the next class.