In [ ]:
# exception handling
# how do we deal with errors.
# ex: ValueError,ZeroDivisionError
# try..except..else..finally
# try -> it hold the code which is mandatory.
# except -> what you want to do with your exceptions
# else -> if all is good in try block , lets go for the else block.
In [1]:
# example1
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
print "the result of our operation is {}".format(num1/num2)
In [2]:
# use case I : error 1
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
print "the result of our operation is {}".format(num1/num2)
In [3]:
# Use case : Error2
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
print "the result of our operation is {}".format(num1/num2)
In [4]:
# python comes with a set of exceptions.
# Can we create our own exception - yes
import exceptions as e
print dir(e)
In [9]:
# exception handling
try:
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
except:
print "please enter numbers and make sure your denominator is non-zero"
else:
print "the result of our operation is {}".format(num1/num2)
In [10]:
# exception handling - we want to only handle the exception below.
# ZeroDivisionError,ValueError
try:
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
except (ZeroDivisionError,ValueError):
print "please enter numbers and make sure your denominator is non-zero"
else:
print "the result of our operation is {}".format(num1/num2)
In [13]:
# exception handling - we want to only handle the exception below.
# ZeroDivisionError,ValueError
try:
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
except ZeroDivisionError:
print "Please make sure your denominator is non-zero."
except ValueError:
print "please enter numbers."
else:
print "the result of our operation is {}".format(num1/num2)
In [14]:
# try..except..else..finally
# try -> it hold the code which is mandatory.
# except -> what you want to do with your exceptions
# else -> if all is good in try block , lets go for the else block.
# finally ->
# case 1: All values are valid - try..else..finally
# case 2: i enter invalid values , but handled by exceptions - try..except..finally
# case 3: i enter invalid values, but not handled by exception - try..finally..exception
In [17]:
try:
num1 = int(raw_input("please enter your num1:" ))
num2 = int(raw_input("please enter your num2:" ))
result = num1/num2
except ValueError:
print "please enter numbers."
else:
print "the result of our operation is {}".format(num1/num2)
finally:
print "All is Well !!!" # close your files,databases,sockets
In [18]:
# raise
raise SyntaxError
In [19]:
raise SyntaxError,"Hey there clean your glasses"
In [20]:
# We can build our own exception.
# you cannot raise all words, the key should be a part of exception class.
In [21]:
raise santosh # is not part of your exception class.
In [23]:
# santosh is a cusotmized exception.
class santosh(Exception):
pass
raise santosh,"I am back!!!"
In [ ]: