In [ ]:
# exceptions - error handling or error out of no where.
# all programming languages have exceptions.

In [1]:
# example 1 - first run

num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "the result is {}".format(result)


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

In [2]:
# example 1 - second run

num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "the result is {}".format(result)


please enter the number1:ten
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-dd4fd454801e> in <module>()
      1 # example 1 - second run
      2 
----> 3 num1 = int(raw_input("please enter the number1:"))
      4 num2 = int(raw_input("please enter the number2:"))
      5 result = num1/num2

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

In [3]:
# example 1 - third run


num1 = int(raw_input("please enter the number1:"))
num2 = int(raw_input("please enter the number2:"))
result = num1/num2
print "the result is {}".format(result)


please enter the number1:10
please enter the number2:0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-710fa15f8c0e> in <module>()
      4 num1 = int(raw_input("please enter the number1:"))
      5 num2 = int(raw_input("please enter the number2:"))
----> 6 result = num1/num2
      7 print "the result is {}".format(result)

ZeroDivisionError: integer division or modulo by zero

In [4]:
# various exeception python can throw.

In [6]:
import exceptions as e

In [7]:
print dir(e) # e.<tab>


['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 [ ]:
# yes, you can build your own exception.

In [8]:
# try.. except .. else .. finally
# try -> try block contains your computation block.
# except -> if you hit on an exception what needs to be done.
# else -> if everything in try block is good.. then go to the else block.

In [9]:
# example 2 - 1st run
try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except:
    print "please enter the numbers.Make sure your denominator is non-zero"
else:
    print "the result is {}".format(result)


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

In [10]:
# example 2 - 2nd run
try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except:
    print "please enter the numbers.Make sure your denominator is non-zero"
else:
    print "the result is {}".format(result)


please enter the number1:ten
please enter the numbers.Make sure your denominator is non-zero

In [11]:
# example 2 - 3rd run
try:
    num1 = int(raw_input("please enter the number1:"))
    num2 = int(raw_input("please enter the number2:"))
    result = num1/num2
except:
    print "please enter the numbers.Make sure your denominator is non-zero"
else:
    print "the result is {}".format(result)


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

In [12]:
# except is a broad term.
# anology:
# what did you eat - food

In [15]:
# example 3 - 1st run
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 the numbers.Make sure your denominator is non-zero"
else:
    print "the result is {}".format(result)


please enter the number1:ten
please enter the numbers.Make sure your denominator is non-zero

In [16]:
# example 4 - 1st run
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 the numbers."
else:
    print "the result is {}".format(result)


please enter the number1:ten
please enter the numbers.

In [17]:
# example 4 - 2nd run
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 the numbers."
else:
    print "the result is {}".format(result)


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

In [ ]:
# try.. except .. else .. finally
# try -> try block contains your computation block.
# except -> if you hit on an exception what needs to be done.
# else -> if everything in try block is good.. then go to the else block.
# finally -> 
# case 1: i enter all the valid values - try-else-finally
# case 2: i enter one invalid value handled by exceptions - try-except-finally
# case 3: i enter one invalid value not handled by exceptions - try-finally-exception error

In [3]:
# example - 1st run - correct values
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 the numbers."
else:
    print "the result is {}".format(result)
finally:
    print "All is well"
    # file close,database close,socket close.


please enter the number1:10
please enter the number2:0
All is well
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-0725e7feebf5> in <module>()
      3     num1 = int(raw_input("please enter the number1:"))
      4     num2 = int(raw_input("please enter the number2:"))
----> 5     result = num1/num2
      6 except ValueError:
      7     print "please enter the numbers."

ZeroDivisionError: integer division or modulo by zero

In [4]:
# raise exceptions.

In [5]:
print "hello world"


hello world

In [6]:
pint "hello world"


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

In [7]:
raise SyntaxError


  File "<string>", line unknown
SyntaxError

In [8]:
raise SyntaxError,"Bubby please clean your glasses"


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

In [9]:
# you can only raise excetion which are part of Exception class.

In [10]:
raise santosh


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

NameError: name 'santosh' is not defined

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

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


---------------------------------------------------------------------------
santosh                                   Traceback (most recent call last)
<ipython-input-12-00736fa6525c> in <module>()
----> 1 raise santosh,"Hey i am back !!!"

santosh: Hey i am back !!!

In [ ]:
# pre-production .
# debugging - tue
# logging - wed/thu
# files - sat
# regular expression - sat/sun
# obeject oriented programming - mon,tue
# sockets - wed
# databases - thu
# CGI - thu
# project - Friday