Handling exceptions.
Hello, everyone.
Today, we're going to learn very interesting things, the first thing and actually the method of this
video is how to handle exceptions and also teach you how to create a custom python file and then run
it from the terminal so you can write a huge amount of code to which later you can simply wrong.
So as you can see, I already created my computer folder called Section two, which is actually the
section in how we structure the sections in discourse.
So in that folder, which specifically for that section, I will actually create another folder called
Resources.
OK, so let's introduce further and let's create a text file, so Olmec, I usually use text editor,
but feel free to use a dictator.
I think Windows is not bad to create a text file.
So here I will create a new document and I will save it as.
Exceptions that few white.
And then the only thing I need to do here is just write.
Don't peel away after it, this is select use, pure white, so if you're a Mac, you need to do the
same thing.
But if you use other devices, you're not going to do that.
So when you open the file.
In your preferred text editor actually here, we're going to use by chance, so I will run it all right
by charm.
So here, once you enter Patro, you can simply select open and go to the file that actually won't open.
So here is the file exceptionals be and select open.
So now you can edit it with a or actually any editor that you would like to use.
So let's right here import.
Eurorail library, not arrow.
And then I will write from you you library to request, and from here I will report that you are ale
open so we can actually open a euro and here's how we do the exceptions.
So first of all, let me tell you why we actually need to do exceptions basically in every programming
language.
And Python is no different.
We need to do exceptions in order to be sure that the program runs correctly, even if we have some
errors.
So it is always better to create an exception for an expected error in order to show the user the exact
quantification and the exact information for fixing that error.
So here's what we do first.
All right.
Tried.
OK.
And try means that are going to try some common, so we will try you, R.L..
Open.
And then the name of the website and here I prepared one website that has on the American side.
So I go to HTP and the website is called w w w dot i e.
T.F..
Dot org.
Ah, F.C..
RNC zero to 60.
OK, so this is the Web page that we're going to assess and in case anything gets wrong and we are not
able to open it, let's write except the URL or.
Library.
Dot ever dot h t v error as e.
OK, so if we get this area here a lot, you can see from there a library in the cold below this area
is going to be expressed with a letter E so I can write print.
And in parentheses, exception.
Slushy, sort of Komaki then print.
Status.
Splash eat dogs called.
OK, then we'll print the reason for their sole reason.
There's reason, Cuomo idot reason and finally will print.
The last line, which will be You are OK and I'll write E!
That you are.
OK, so here, if we get this type of error HTP error, then we're going to show plenty of information
for it.
Instead of simply displaying the error for the user, let's save that.
So I would control errors and let's actually run the code.
So I'll simply go to the terminal and I'll go to the folder.
So so I will go to Section two.
And with and here we have the resources, the resources folder, and you can see the exception, why
is there so you can simply do Python and do the right exceptional way.
So while around the fire, you can see that we've got the exception, HTP error.
OK.
And.
This means that the page is not found.
OK, so this is what he would get if we don't add the additional lines with the exception, but rather
the exception, we can see the status is 404, which is called.
The reason is that it is not found the Eurorail, because here you can see we not have the euro.
The euro is the.
This one, the IETF dot org, so you can see all this information and you can actually add more information
if you desire, so you can see this for less, of course, when you get on there and see if we do not
have the exception.
For example, if we don't choose this quote, but they simply write, you are open.
And then right now the U.S. actually copy.
So if I just copy this here, I basically get in some mode and they see nothing about the fact that
we actually got any error related to what we just did.
But since we get the exception, we have this exception.
If any issue happens, you can always see the error message.
And what are the details regarding that message?
So this is quite useful.
So this is everything I want to share with you guys about exceptions.
I hope you enjoyed this video and you actually learn how to run Python codes, which in your term know
machine, which quite well, because we are going to use this extra quite a lot.
That said, thank you very much for watching, guys.
And tomorrow we'll continue with that with the HTP Heathers.
Thanks for watching.