Welcome to CCP
Lets get started !
So, in last part we made a web server which is a very cool thing,
Now, lets focus on yet another basic concept called Error Handling,
it focuses on how to explicitly or willingly silence errors which can cause our program to crash,
when a crash occurs, the program stops executing,
like this:
[Used PyCharm]
It is common for program to have some errors and not all are needed to be debugged,
sometimes, errors come in random patterns and are not so critical,
thus,
only when the errors are not critical,
we should use try and except statements
for example:
Suppose you are hosting a site and made a python program to make GET requests to your site to check whether its working or not,
here you can use try...except blocks to ensure that errors like requestTimeout should not crash your program due to slow bandwidth or internet and can try making this request till the website sends you a correct response,
there are lots and lots of ideas of how you can use these code blocks,
Now,
here is a program similar the the program we made earlier but is now equipped with try...except blocks:
Here, we can see that like while and for loops or if-else statements, these blocks also contain with a semi-colon and indentation to mark the body of the try...except block,
Notice the line 5, we can use the Exception keyword to make it work for any error or exception,
and we are also using the as keyword which i think you guessed it,
yes, it is used to assign the e variable to the class of the error,
when we print e, due to its __str__() method, it prints the error message of the error as string,
to be specific, we have to replace the Exception with the error name like ZeroDivisionError and many more,
like this:
When we specifically determine the error it also works but only works when error ZerodivisionError happens else it produces an output like this:
As the error handling statements were specifically designed for ZeroDivisionError and not for NameError which happens when we use a variable which is not defined,
This issue can be resolved by adding another except statement which specifically is used for NameError,
that was simple huh ?
lets experiment with this by adding both the error statements,
which caused NameError and which caused ZerodivisionError,
like this:
Whoa !
Didn't you expected both statements as their both handling exceptions were there ?
Explanation:
This happened because the code declares that it is an error on observing the error immediately and not even goes to next line,
as this error was placed in try block, the try block searched for exception that matches the error type immediately after encountering the error without moving to the next line,
as there was an exception for the error, it printed the error line we defined in the except block,
Now, you should be ready to use the try...except block in your server's code that we deployed in the previous part and i told you to ignore for now in the line-by-line explanation,
Today's summary:
Errors should never pass silently.
Unless explicitly silenced.
With that said, lets call it a class,
meet you, in the next part !
This is amazing ! you give tutorials with almost ground level and then do the magic which gives me more understanding about the subject, Thank friend you give tutorials better than anyone
ReplyDelete