As you continue to progress in your journey as
a Python programmer or as a programmer in general,
there is a habit that I want you to adopt and this habit
is gonna involve writing programs or designing programs
that are able to handle potential errors
in a graceful manner.
So the thing is most programmers out there,
they create all these kinds of great programs that can do this to do that.
However, if something goes wrong,
maybe the user of the program added some type of an input
or maybe they used numbers instead of letters or something like that,
you know,
many times these programs end up crashing because the programmer
hasn't
designed a program in such a way
that if such potential errors do occur,
the program knows how to handle it.
We have talked about handling errors briefly when we talked about loops,
but now we're going to go a bit more in depth
on my screen right now.
I have a very simple program that reads the file, the example dot CXT.
But note however, that this program, the short block of code,
it relies on this file example dot TXT actually existing
what if
I maybe made a mistake or the use of the program,
the user of the program, you made a mistake.
And instead of example dot txt, we have example dot txt.
Now this file doesn't exist. It's not in my directory.
If I run the program, now, you can see we have an arrow.
Python is telling us that, hey, sorry, no such file or directory file,
not found error.
This doesn't look good, right? So
we want to write our programs in such a way that if such an error does occur,
it's going to handle it in a very graceful and professional manner. So
what we do right now is let me go back in here and add example
dot txt. OK?
So
we're gonna use the try except
block.
We've talked about this when we talked about loops, but
let's talk about it again. So
what you want to do is
in the block of code where there's a potential for the error to occur
such as you know, over here,
one thing you want to do is you want to start off by saying try,
try and now make sure it's indented properly because
the code after try should be under it.
So now we're saying, OK,
try to open this file example dot txt.
And if it exists and all that,
you can print out the contents of the file,
OK?
However,
what if the file does not exist now, we can say accept
and then under accept,
we can say print
and then we can say something like uh file not found.
OK,
let's add a colon where
accept
and there it is. So basically we're saying, hey,
try to run this block of code
and if for one reason or the other,
the file isn't found and simply print out file, not found. So
if I run the program right now,
everything works properly because the file does exist.
However,
if I change the name of the file
and I run the program,
you can see very professionally. It simply says file not found.
And that's because we're using the try except block
while this works well
to go deeper.
I want you to start using
what we call the common file related exceptions. There's quite a variety of them
and Python has specific exceptions
for
many of the common file related errors.
The the first one and the most common one is the file,
not found error just as we have over here.
So
all you need to do is instead of just simply saying, try except
where you have the accept,
you can type in file,
not found error.
This right here,
we're telling Python to specifically
check to see if the error is caused by the fact that the file was not found.
So
if I run my program again, right now, you can see it still works perfectly fine.
However,
this is a much cleaner and a more efficient way
of handling the error. Because over here
we're specifically checking to see if the
error occurred because the file doesn't exist
the try except block on its own handles all errors,
whether the file wasn't found,
whether it's,
it's a permission error or maybe it's a directory
error or maybe it's an open system error.
It will handle everything. So
yes, it will work quite a lot. It will handle the error.
But again, a cleaner way will be to specify
the kind of error that you are actually expecting. So
we have the file, not found error.
We also have the permission error.
Maybe for example,
the
uh the file in question is can only be accessed by an admin.
And now it's trying to be accessed by a regular user.
Obviously, you're going to have the error saying, hey,
so you don't have permission. So in
this kind of scenario, all we'll simply do is we'll just say,
you know, sorry you do, you do not have permission
to view this file,
you know, and that's all we would need to do.
So if you're expecting a permission error,
you want to go with the accept permission error, block of code.
We also have the is a directory error.
Maybe the user was trying to open up a directory that doesn't exist again,
simply come in here
and then you type in
is a
directory error and there it is. And now from here,
we can specify the exact print message. So in
this case, it will be something like sorry,
you don't have permission to access this directory or sorry.
This directory doesn't exist, you know, something like that. And then the last one
is going to be the operating system error.
So this is typically the general Exception for other file
related errors.
Maybe the disk is full, maybe it's an input output issue, something like that.
So
all you need to do right now is just say accept
and then
OS
arrow
and then you can just type in something like sorry,
an error has occurred or an open system error has occurred, something like that.
That's exactly how that would work. So
once again, you want to adopt this
professional approach towards
constantly
making sure that your blocks of code.
Whenever there's a possibility of an error occurring,
they have been designed in such a way
that they're able to handle any potential errors
by making use of the try except block. And then for the accept statement itself,
you can specify the exact kind of error
that you expect might occur. Thank you for watching. I will see you in the next class.