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!

0 comments:

Post a Comment