Showing posts with label While True. Show all posts
Showing posts with label While True. Show all posts

Saturday, April 21, 2012

Error Handling Part 3


Hello everyone in this post, I will discussing something more about the Error Handling, So now we should know how to Handle Error with Try and Except, and we have also seen the use of while statement to keep our program alive.

Now here is something new, we will continue with our old script, div.py, so lets open up the script:

import exceptions
while True:
    try:
        a = int (input("Enter First Num: "))
        b = int (input("Enter Second Num: "))
        print "Result: ", a/b
        break
    except SyntaxError:
        print "Error With Your Syntax"
    except NameError:
        print "Idiot, I dont Understand You"
    except ZeroDivisionError:
        print "Integer cannot be divided by me with Zero"
    except TypeError:
        print "Hmm...Sumthingz Fishy Here"
    except KeyboardInterrupt:
        print "Hey You aborted Me"
print "------------------------"
print "[+] EOF Script"


So this worked fine in the last, and this is fine if the script a small one like this one, what if you have a huge script where you have to handle many Errors, the length of the program would just keep increasing, so we wrap the common ones or similar ones in parenthesis under one except block, remember, we you write a script, try to make the script as small as possible, and also the code should be readable, now our modified script would be:

import exceptions
while True:
    try:
        a = int (input("Enter First Num: "))
        b = int (input("Enter Second Num: "))
        print "Result: ", a/b
        break
    except (SyntaxError, NameError, ZeroDivisionError, TypeError, KeyboardInterrupt):
        print "Error: Session Aborted"
print "------------------------"
print "[+] EOF Script"

So we see here that we have placed all the exceptions errors under a single except block. so this script would display "Error: Session Aborted" for every the errors. Now this is just to show you, the way you handle the errors for your script depends on the business needs or probably the behavior of your script.

Another thing to discuss in this post, assume you are writing a huge script, its not possible to type a customed error every time for every error. So we display the error that Pythons throws, but after handling the exception. Working again with the same script:

import exceptions
while True:
    try:
        a = int (input("Enter First Num: "))
        b = int (input("Enter Second Num: "))
        print "Result: ", a/b
        break
    except (SyntaxError, NameError, ZeroDivisionError, TypeError, KeyboardInterrupt) as e:
        print "Error: ", e
print "------------------------"
print "[+] EOF Script"

On executing the script, we would get a short error message as to why the error occurred. Try this out.

Thats all for this post, hope it was informative.

Thank You!

Friday, April 20, 2012

Error Handling Part 2


Ok so now that we know about Try and Except, we will see something Interesting. Well we obviously will not want our program to get halt at any error, from last post we know how to handle the errors, now in this post I will show how to handle the errors and then continuing the script, thus giving you an idea of a full blown Python Script or Application.

Lets take the same script from the last post. So open up the div.py and we will modify the code as below:

import exceptions
try:
    a = int (input("Enter First Num: "))
    b = int (input("Enter Second Num: "))
    print "Result: ", a/b
except SyntaxError:
    print "Error With Your Syntax"
except NameError:
    print "Idiot, I dont Understand You"
except ZeroDivisionError:
    print "Integer cannot be divided by me with Zero"
except TypeError:
    print "Hmm...Sumthingz Fishy Here"
except KeyboardInterrupt:
    print "Hey You aborted Me"

Now thats the main script, which can only handle the errors, but wont keep continuing the script, to achieve this we need a while loop with break statements. So our modified code would be as below:

import exceptions
while True:
    try:
        a = int (input("Enter First Num: "))
        b = int (input("Enter Second Num: "))
        print "Result: ", a/b
        break
    except SyntaxError:
        print "Error With Your Syntax"
    except NameError:
        print "Idiot, I dont Understand You"
    except ZeroDivisionError:
        print "Integer cannot be divided by me with Zero"
    except TypeError:
        print "Hmm...Sumthingz Fishy Here"
    except KeyboardInterrupt:
        print "Hey You aborted Me"
print "------------------------"
print "[+] EOF Script"

On executing the script:

>>>
Enter First Num:
Error With Your Syntax
Enter First Num: str
Hmm...Sumthingz Fishy Here
Enter First Num:
Hey You aborted Me
Enter First Num: 40
Enter Second Num: 0
Result:  Integer cannot be divided by me with Zero
Enter First Num: lol
Idiot, I dont Understand You
Enter First Num: 40
Enter Second Num: 2
Result:  20
------------------------
[+] EOF Script
>>>

So let me explain the script, I have kept the main try and except blocks inside a While loop, and the condition says, while True, means keep doing it, until you find a break statement, A break statement exits a loop. And inside the try block I have placed the break statement, so that the loop can exit only if the try is a success, if its not a success and if there is any error, it goes to the except statement, and since the except statements doesn't have a break statement, the script would loop again because of the while True condition. True is a builtin keyword. You can also replace True with 1. So it also be coded as

while 1:
     #scripts
This would give the same result. I prefer typing True, because it looks more Pythonist.

Okay, so I guess this post should be an interesting one. Hope you are learning Python from the blog. Keep Practicing, and don't forget to check my next post.

Thank You!