In [ ]:
# exceptions: error handling
# https://docs.python.org/2.7/tutorial/errors.html
# pre-production
In [1]:
# example 1:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "result is {}".format(result)
In [3]:
# exception 1
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "result is {}".format(result)
In [4]:
# exception 2:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "result is {}".format(result)
In [5]:
# what are different exceptions in python
import exceptions as e
In [6]:
print dir(e)
In [ ]:
# try..except..else..finally
# try -> to compute or most important task.
# except -> handling your exceptions.
# else -> if try is True.. then go to else.
In [7]:
# run1
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except:
print "please enter numbers.Make sure your denominator is non-zero"
else:
print "result is {}".format(result)
In [8]:
# run2
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except:
print "please enter numbers.Make sure your denominator is non-zero"
else:
print "result is {}".format(result)
In [9]:
# run3
try:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
except:
print "please enter numbers.Make sure your denominator is non-zero"
else:
print "result is {}".format(result)
In [10]:
# now lets tell crearly that its a value error or a zero division error.
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 numbers.Make sure your denominator is non-zero"
else:
print "result is {}".format(result)
In [11]:
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 numbers.Make sure your denominator is non-zero"
else:
print "result is {}".format(result)
In [12]:
# making it more granular.
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 numbers."
else:
print "result is {}".format(result)
In [14]:
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 numbers."
else:
print "result is {}".format(result)
In [ ]:
# hhow about an exception which is not handled in the program.
# try..except..else..finally
# try -> to compute or most important task.
# except -> handling your exceptions.
# else -> if try is True.. then go to else.
# finally->
# case1: when i enter all values which are valid. -> try..else..finally
# case2: when i enter invalid value, handled by exception.-> try..except..finally
# case3: when i enter invalid values,not handed by exception -> try..finally..bombed with exception
In [17]:
#case1
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 numbers."
else:
print "result is {}".format(result)
finally:
print "we are all good."
In [18]:
#case2
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 numbers."
else:
print "result is {}".format(result)
finally:
print "we are all good."
In [19]:
#case3
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 numbers."
else:
print "result is {}".format(result)
finally:
print "we are all good."
In [ ]:
# finally block is to make sure you can close all resource before code exits.
# db.close
# socket.close
# file.close
In [ ]:
# raise
In [20]:
pint "hello welcome "
In [22]:
raise SyntaxError,"please clean your glasses."
In [ ]:
# if you want to raise an exception it has to be part of exception class.
In [23]:
raise santosh
In [24]:
# lets make santosh part of exception class
class santosh(Exception):
pass
In [25]:
raise santosh,"I am back!!!"
In [26]:
#!/usr/bin/env python
age = raw_input("please enter the age 1-150:")
class InvalidAgeRangeException(Exception):
def __init__(self,age):
self.age = age
def validate_age(age):
if age < 0 or age > 150:
raise InvalidAgeRangeException(age)
# Main
try:
age=int(age)
validate_age(age)
except ValueError as e:
print e
except InvalidAgeRangeException as e:
print 'Invalid age range, you entered : %s' % e.age
In [ ]: