In [ ]:
# exceptions - error handling or error out of no where.
# all programming languages have exceptions.
In [1]:
# example 1 - first run
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "the result is {}".format(result)
In [2]:
# example 1 - second run
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "the result is {}".format(result)
In [3]:
# example 1 - third run
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "the result is {}".format(result)
In [4]:
# various exeception python can throw.
In [6]:
import exceptions as e
In [7]:
print dir(e) # e.<tab>
In [ ]:
# yes, you can build your own exception.
In [8]:
# try.. except .. else .. finally
# try -> try block contains your computation block.
# except -> if you hit on an exception what needs to be done.
# else -> if everything in try block is good.. then go to the else block.
In [9]:
# example 2 - 1st run
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except:
print "please enter the numbers.Make sure your denominator is non-zero"
else:
print "the result is {}".format(result)
In [10]:
# example 2 - 2nd run
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except:
print "please enter the numbers.Make sure your denominator is non-zero"
else:
print "the result is {}".format(result)
In [11]:
# example 2 - 3rd run
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except:
print "please enter the numbers.Make sure your denominator is non-zero"
else:
print "the result is {}".format(result)
In [12]:
# except is a broad term.
# anology:
# what did you eat - food
In [15]:
# example 3 - 1st run
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except (ZeroDivisionError,ValueError):
print "please enter the numbers.Make sure your denominator is non-zero"
else:
print "the result is {}".format(result)
In [16]:
# example 4 - 1st run
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except ZeroDivisionError:
print "Make sure your denominator is non-zero"
except ValueError:
print "please enter the numbers."
else:
print "the result is {}".format(result)
In [17]:
# example 4 - 2nd run
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except ZeroDivisionError:
print "Make sure your denominator is non-zero"
except ValueError:
print "please enter the numbers."
else:
print "the result is {}".format(result)
In [ ]:
# try.. except .. else .. finally
# try -> try block contains your computation block.
# except -> if you hit on an exception what needs to be done.
# else -> if everything in try block is good.. then go to the else block.
# finally ->
# case 1: i enter all the valid values - try-else-finally
# case 2: i enter one invalid value handled by exceptions - try-except-finally
# case 3: i enter one invalid value not handled by exceptions - try-finally-exception error
In [3]:
# example - 1st run - correct values
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except ValueError:
print "please enter the numbers."
else:
print "the result is {}".format(result)
finally:
print "All is well"
# file close,database close,socket close.
In [4]:
# raise exceptions.
In [5]:
print "hello world"
In [6]:
pint "hello world"
In [7]:
raise SyntaxError
In [8]:
raise SyntaxError,"Bubby please clean your glasses"
In [9]:
# you can only raise excetion which are part of Exception class.
In [10]:
raise santosh
In [11]:
class santosh(Exception):
pass
In [12]:
raise santosh,"Hey i am back !!!"
In [ ]:
# pre-production .
# debugging - tue
# logging - wed/thu
# files - sat
# regular expression - sat/sun
# obeject oriented programming - mon,tue
# sockets - wed
# databases - thu
# CGI - thu
# project - Friday