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)


please enter the number one:10
please enter the number two:2
the division of two numbers is:5

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)


please enter the number one:ten
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-b11a917e7aa1> in <module>()
----> 1 num1 = int(raw_input("please enter the number one:"))
      2 num2 = int(raw_input("please enter the number two:"))
      3 result = num1 / num2
      4 print "the division of two numbers is:{}".format(result)

ValueError: invalid literal for int() with base 10: 'ten'

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)


please enter the number one:10
please enter the number two:0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-b11a917e7aa1> in <module>()
      1 num1 = int(raw_input("please enter the number one:"))
      2 num2 = int(raw_input("please enter the number two:"))
----> 3 result = num1 / num2
      4 print "the division of two numbers is:{}".format(result)

ZeroDivisionError: integer division or modulo by zero

In [12]:
# what are the various excpetion python support
import exceptions as e
print dir(e)


['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__doc__', '__name__', '__package__']

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)


please enter the number one:ten
Please enter numbers. Make sure your denominator is non-zero

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)


please enter the number one:10
please enter the number two:0
Please enter numbers. Make sure your denominator is non-zero

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)


please enter the number one:10
please enter the number two:2
the division of two numbers is:5

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)


please enter the number one:ten
Please enter numbers. Make sure your denominator is non-zero

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)


please enter the number one:tten
Please enter numbers.

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)


please enter the number one:10
please enter the number two:0
Make sure your denominator is non-zero

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."


please enter the number one:10
please enter the number two:2
the division of two numbers is:5
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."


please enter the number one:ten
Please enter numbers.
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()


please enter the number one:10
please enter the number two:0
hello we are done.
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-16-851c37652c22> in <module>()
      3     num1 = int(raw_input("please enter the number one:"))
      4     num2 = int(raw_input("please enter the number two:"))
----> 5     result = num1 / num2
      6 except ValueError:
      7     print "Please enter numbers."

ZeroDivisionError: integer division or modulo by zero

In [17]:
# raise
pint "hello world"


  File "<ipython-input-17-61cb1b62bdcd>", line 2
    pint "hello world"
                     ^
SyntaxError: invalid syntax

In [20]:
raise SyntaxError,"Please clean your glasses"


  File "<string>", line unknown
SyntaxError: Please clean your glasses

In [21]:
raise santosh


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-460ec4d018fb> in <module>()
----> 1 raise santosh

NameError: name 'santosh' is not defined

In [23]:
class santosh(Exception):
    pass

In [24]:
raise santosh,"i am back!!"


---------------------------------------------------------------------------
santosh                                   Traceback (most recent call last)
<ipython-input-24-58a423ded1d3> in <module>()
----> 1 raise santosh,"i am back!!"

santosh: i am back!!

In [ ]: