In [ ]:
# exceptions,exception handling.
# Costliest errors.
In [ ]:
# example
In [1]:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print "results of the operation is {}".format(result)
In [2]:
# case I
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print "results of the operation is {}".format(result)
In [3]:
# case II
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print "results of the operation is {}".format(result)
In [6]:
import exceptions as e
In [7]:
print dir(e)
In [ ]:
# try .. except .. else .. finally
# try -> computation block or important block.
# except -> handled the exceptions.
# else -> if all is good in try block .. then go for else block.
In [8]:
try:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except:
print "please make sure you enter numbers.Make sure denomimator is non-zero"
else:
print "results of the operation is {}".format(result)
In [9]:
# case 2
try:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except:
print "please make sure you enter numbers.Make sure denomimator is non-zero"
else:
print "results of the operation is {}".format(result)
In [10]:
# case 3
try:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except:
print "please make sure you enter numbers.Make sure denomimator is non-zero"
else:
print "results of the operation is {}".format(result)
In [11]:
# to narrow down to our exceptions
try:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except (ZeroDivisionError,ValueError):
print "please make sure you enter numbers.Make sure denomimator is non-zero"
else:
print "results of the operation is {}".format(result)
In [12]:
#How to handle customized exceptions
try:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except ZeroDivisionError:
print "Make sure your denominator is non-zero."
except ValueError:
print "please make sure you enter numbers."
else:
print "results of the operation is {}".format(result)
In [14]:
print "hello world
In [ ]:
# finally
# case I: I enter all valid values .. try..else..finally.
# case II: I enter invalid values.. handled by exceptions .. try..except..finally
# case III: I enter invalid values.. not handled by exceptions..try..finally..exception issue.
In [17]:
try:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except ValueError:
print "please make sure you enter numbers."
else:
print "results of the operation is {}".format(result)
finally:
print "close your resource here."
# database.close
# socket.close
# file.close
In [18]:
# raise
raise SyntaxError
In [19]:
raise SyntaxError,"Buddy!! please clean your glasses."
In [20]:
# your cannot raise every other word.
# santosh needs to be part of the exception class.
raise santosh
In [ ]: