In [ ]:
# exception handling
# pre-production
# try..except..else.. finally
# try: computation part/must part of the code.
# except: for handing the exceptions
# else: if try is true.. then go to else.
#
In [1]:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
print "the division of two numbers is:{}".format(result)
In [2]:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
print "the division of two numbers is:{}".format(result)
In [3]:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
print "the division of two numbers is:{}".format(result)
In [12]:
# what are the various excpetion python support
import exceptions as e
print dir(e)
In [4]:
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except:
print "Please enter numbers. Make sure your denominator is non-zero"
else:
print "the division of two numbers is:{}".format(result)
In [5]:
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except:
print "Please enter numbers. Make sure your denominator is non-zero"
else:
print "the division of two numbers is:{}".format(result)
In [6]:
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except:
print "Please enter numbers. Make sure your denominator is non-zero"
else:
print "the division of two numbers is:{}".format(result)
In [7]:
# Handing multiple exception in one go.
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except (ValueError,ZeroDivisionError):
print "Please enter numbers. Make sure your denominator is non-zero"
else:
print "the division of two numbers is:{}".format(result)
In [8]:
# handling each excpetion seperately.
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except ValueError:
print "Please enter numbers."
except ZeroDivisionError:
print "Make sure your denominator is non-zero"
else:
print "the division of two numbers is:{}".format(result)
In [9]:
#
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except ValueError:
print "Please enter numbers."
except ZeroDivisionError:
print "Make sure your denominator is non-zero"
else:
print "the division of two numbers is:{}".format(result)
In [ ]:
# try..except..else.. finally
# try: computation part/must part of the code.
# except: for handing the exceptions
# else: if try is true.. then go to else.
# finally:
# case1: when all values are entered correctly -> try..else..finally.
# caseII: when we enter invalid value handled by exceptions -> try..except..finally
# caseIII: when we enter invalid value not handled by exception -> try..finally.. bombed with exception.
In [14]:
# case I
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except ValueError:
print "Please enter numbers."
else:
print "the division of two numbers is:{}".format(result)
finally:
print "hello we are done."
In [15]:
# case II
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except ValueError:
print "Please enter numbers."
else:
print "the division of two numbers is:{}".format(result)
finally:
print "hello we are done."
In [16]:
# case III
try:
num1 = int(raw_input("please enter the number one:"))
num2 = int(raw_input("please enter the number two:"))
result = num1 / num2
except ValueError:
print "Please enter numbers."
else:
print "the division of two numbers is:{}".format(result)
finally:
print "hello we are done."
# db.close()
# socket.close()
# file.close()
In [17]:
# raise
pint "hello world"
In [20]:
raise SyntaxError,"Please clean your glasses"
In [21]:
raise santosh
In [23]:
class santosh(Exception):
pass
In [24]:
raise santosh,"i am back!!"
In [ ]: