Thursday, April 19, 2012

Error Handling Part 1


Hello Everyone, in this post, I will discuss, about handling Error In Python. Well as a programmer, we want our script or application to run absolutely fine, without any error or warnings, making the program halt due to the error.

So It is the most important part of any programming language to handle errors. So Lets see how to Handle errors, but before lets see what all error can a simple program have:

print "Division Script"
a = int (input("Enter First Num: "))
b = int (input("Enter Second Num: "))
print "Result is: ", a/b

Save this as div.py and when we execute the program, remember we are focusing on getting Errors, so we will Generate Errors.

>>>
Division Script
Enter the First Num: 10.1
Enter the Second Num: 2
Result is:  5
>>> ============= RESTART ===============
>>>
Division Script
Enter the First Num: str

Traceback (most recent call last):
  File "C:/Python27/div1.py", line 2, in <module>
    a = int(input("Enter the First Num: "))
TypeError: int() argument must be a string or a number, not 'type'
>>> ============= RESTART ===============
>>>
Division Script
Enter the First Num: lol

Traceback (most recent call last):
  File "C:/Python27/div1.py", line 2, in <module>
    a = int(input("Enter the First Num: "))
  File "<string>", line 1, in <module>
NameError: name 'lol' is not defined
>>> ============= RESTART ===============
>>>
Division Script
Enter the First Num: 40
Enter the Second Num: 0
Result is:

Traceback (most recent call last):
  File "C:/Python27/div1.py", line 4, in <module>
    print "Result is: ", a/b
ZeroDivisionError: integer division or modulo by zero
>>> ============= RESTART ===============
>>>
Division Script
Enter the First Num:

Traceback (most recent call last):
  File "C:/Python27/div1.py", line 2, in <module>
    a = int(input("Enter the First Num: "))
  File "<string>", line 0
   
   ^
SyntaxError: unexpected EOF while parsing
>>> ============= RESTART ===============
>>>
Division Script
Enter the First Num:

Traceback (most recent call last):
  File "C:/Python27/div1.py", line 2, in <module>
    a = int(input("Enter the First Num: "))
KeyboardInterrupt
>>>
So here we have quite a few Error, which we never looked at earlier while programming. now we will handle those errors.So we will modify our script:

import exceptions <-- Module to Handle Error and Warning 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"

And when we execute this script, and try to generate errors, we will get the customed messages, rather than a Traceback Error.

Ok let me explain the script first, first we imported the Module, then our main script is inside the try block, try block asks Python to try something, and if it fails or generates some error, this error will be passed to the except blocks, and if a matched except block is found, it will execute the codes under that particular except block. Also note here, I am very specific about the error types because This is a small program, and the chances of Error are limited here, what I could have done is, I could have made a global except block that would have handled any error. But its actually a bad practice, to house all errors under one except statement. Just To show you, I could have done this:

import exceptions <-- Module to Handle Error and Warning Exceptions
try:
    a = int (input("Enter First Num: "))
    b = int (input("Enter Second Num: "))
    print "Result: ", a/b
except:
    print "There is Some Error"

So this would show the same error for every kind of errors, But we don't want that, okay so lets run our modified script:

>>> ============= RESTART ===============
>>>

Enter First Num: 40
Enter Second Num: 0
Result:  Integer cannot be divided by me with Zero
>>> ============= RESTART ===============
>>>
Enter First Num: str
Hmm...Sumthingz Fishy Here
>>> ============= RESTART ===============
>>>
Enter First Num: lol
Idiot, I dont Understand You
>>> ============= RESTART ===============
>>>
Enter First Num:
Hey You aborted Me
>>> ============= RESTART ===============
>>>
Enter First Num:
Error With Your Syntax
>>>

Okay, This is all in this post, will discuss more with Error Handling in the next Post. I hope this was interesting. Don't Forget to check my next post.

Thank You!

0 comments:

Post a Comment