In [ ]:
# exception are error handling in programming.
In [1]:
# use case 1:
num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print result
In [2]:
# error1:
num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print result
In [3]:
# error 2
num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print result
In [4]:
# what are different exception python handles.
import exceptions as e
In [5]:
print dir(e)
In [ ]:
# try : code which is mandatory(must).
# except: handles your exceptions.
# else: if the try block is true. what we need to do.
# finally
In [7]:
## resolving errors
## use case 1:
# except is below example is very broad, which mean it covers all exceptions.
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 integers and your denominator is non-zero"
else:
print result
In [8]:
## use 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 integers and your denominator is non-zero"
else:
print result
In [9]:
## use 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 integers and your denominator is non-zero"
else:
print result
In [ ]:
## use the exception handles in code.
In [10]:
## use case 1:
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 integers."
except ZeroDivisionError:
print "Please make sure your denominator is non-zero"
else:
print result
In [12]:
## use case 2:
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 integers."
except ZeroDivisionError:
print "Please make sure your denominator is non-zero"
else:
print result
In [ ]:
## use tuple of exceptions
In [15]:
## here we are not covering all exception.
## we are only dealing with ValueError and ZeroDivisionError
try:
num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
except (ValueError,ZeroDivisionError):
print "please make sure you enter integers.Please make sure your denominator is non-zero"
else:
print result
In [ ]:
# try : code which is mandatory(must).
# except: handles your exceptions.
# else: if the try block is true. what we need to do.
# finally:
# case1 : i will enter all valid values .. try.. else.. finally.
# case2 : i will enter an invalid value.. handled by exceptions .. try..except..finally.
# case3 : i will enter invalid values .. not handled by exceptions.. try..finally..bombed out of the program.
In [19]:
## what if we have not handled an exception in our code.
## how do we deal with this?
## finally block make sure you close all your resource in sane way..before bombing out of an program.
## file.close,socket.close,db.close.
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 integers."
else:
print result
finally:
print "we are all good"
# db.close
# file.close
# socket.close
In [ ]:
## raise keyword.
# All your exception are part of Exception class.
# raise keyword will only work on exception which are part of Exception class
In [20]:
raise SyntaxError
In [21]:
# comment to the exception.
raise SyntaxError,"Please clean your glasses"
In [22]:
raise santosh
In [24]:
## so how to make santosh part of exeption class.
## note: OOP is yet to come do relax.
# we will deal with our own custome exception when we deal with OOP.(classes)
class santosh(Exception):
pass
In [25]:
raise santosh,"hey i am back!!!"
In [ ]: