In [ ]:
# excetions
# you want your program not to exit.
# providing more meaningful error messages.
# try.. except .. else .. finally
# try => this is for computation part.
# except => for handling exceptions.
# else => if try is TRUE.. then go to else part.
# finally =>
# case1 : if i am entering all valid values. try.. else.. finally.
# case2 : if i am entering invalid values.. handled by exceptions .. try .. except.. finally.
# case3 : if i am entring invalid values.. not handled by exceptions..try.. finally .. exception.

In [4]:
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 [1]:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "result of the value is {}".format(result)


please enter the number1:10
please enter the number2:2
result of the value is 5

In [2]:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "result of the value is {}".format(result)


please enter the number1:ten
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-13f897dd6dba> in <module>()
----> 1 num1 = int(raw_input("please enter the number1:"))
      2 num2 = int(raw_input("please enter the number2:"))
      3 result = num1/num2
      4 print "result of the value is {}".format(result)

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

In [3]:
num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "result of the value is {}".format(result)


please enter the number1:10
please enter the number2:0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-13f897dd6dba> in <module>()
      1 num1 = int(raw_input("please enter the number1:"))
      2 num2 = int(raw_input("please enter the number2:"))
----> 3 result = num1/num2
      4 print "result of the value is {}".format(result)

ZeroDivisionError: integer division or modulo by zero

In [5]:
# example1
# except: is a broader sense of dealing with exceptions.

try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except:
    print "you need to enter numbers. And make sure denominator is non-zero."
else:
    print "result of the value is {}".format(result)


please enter the number1:ten
you need to enter numbers. And make sure denominator is non-zero.

In [6]:
# example2
# except: is a broader sense of dealing with exceptions.

try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except (ValueError,ZeroDivisionError):
    print "you need to enter numbers. And make sure denominator is non-zero."
else:
    print "result of the value is {}".format(result)


please enter the number1:10
please enter the number2:0
you need to enter numbers. And make sure denominator is non-zero.

In [7]:
# example3
# except: is a broader sense of dealing with exceptions.

try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except ValueError:
    print "you need to enter numbers"
except ZeroDivisionError:
    print "Make sure denominator is non-zero."
else:
    print "result of the value is {}".format(result)


please enter the number1:10
please enter the number2:0
Make sure denominator is non-zero.

In [8]:
# example4 : capturing errros.
# except: is a broader sense of dealing with exceptions.

try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except ValueError,error:
    print "you need to enter numbers"
    print error
except ZeroDivisionError,error:
    print "Make sure denominator is non-zero."
    print error
else:
    print "result of the value is {}".format(result)


please enter the number1:10
please enter the number2:0
Make sure denominator is non-zero.
integer division or modulo by zero

In [12]:
# example5 : capturing errros.
# except: is a broader sense of dealing with exceptions.

try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except ValueError:
    print "you need to enter numbers"
else:
    print "result of the value is {}".format(result)
finally:
    print "hey we are good now."
    # db.close()
    # files.close()
    # sockets.close()


please enter the number1:10
please enter the number2:0
hey we are good now.
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-12-6159544ad628> in <module>()
      5     num1 = int(raw_input("please enter the number1:"))
      6     num2 = int(raw_input("please enter the number2:"))
----> 7     result = num1/num2
      8 except ValueError:
      9     print "you need to enter numbers"

ZeroDivisionError: integer division or modulo by zero

In [15]:
# raise
#to raise an exception , it has to be part of the exception class.
raise SyntaxError,"buddy!!! clean your spects."


  File "<string>", line unknown
SyntaxError: buddy!!! clean your spects.

In [16]:
pint "hello world"


  File "<ipython-input-16-48713df50b5d>", line 1
    pint "hello world"
                     ^
SyntaxError: invalid syntax

In [17]:
raise santosh


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

NameError: name 'santosh' is not defined

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

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


---------------------------------------------------------------------------
santosh                                   Traceback (most recent call last)
<ipython-input-19-1d43533526c1> in <module>()
----> 1 raise santosh,"hey i am back!!!"

santosh: hey i am back!!!

In [ ]: