In [ ]:
# exceptions,exception handling.
# Costliest errors.

In [ ]:
# example

In [1]:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print "results of the operation is {}".format(result)


please enter the num1:10
please enter the num2:2
results of the operation is 5

In [2]:
# case I
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print "results of the operation is {}".format(result)


please enter the num1:ten
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-8a36193a78f0> in <module>()
      1 # case I
----> 2 num1 = int(raw_input("please enter the num1:"))
      3 num2 = int(raw_input("please enter the num2:"))
      4 result = num1/num2
      5 print "results of the operation is {}".format(result)

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

In [3]:
# case II
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print "results of the operation is {}".format(result)


please enter the num1:10
please enter the num2:0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-6bcdbb17d840> in <module>()
      2 num1 = int(raw_input("please enter the num1:"))
      3 num2 = int(raw_input("please enter the num2:"))
----> 4 result = num1/num2
      5 print "results of the operation is {}".format(result)

ZeroDivisionError: integer division or modulo by zero

In [6]:
import exceptions as e

In [7]:
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 [ ]:
# try .. except .. else .. finally
# try -> computation block or important block.
# except -> handled the exceptions.
# else -> if all is good in try block .. then go for else block.

In [8]:
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 numbers.Make sure denomimator is non-zero"
else:
    print "results of the operation is {}".format(result)


please enter the num1:10
please enter the num2:2
results of the operation is 5

In [9]:
# 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 numbers.Make sure denomimator is non-zero"
else:
    print "results of the operation is {}".format(result)


please enter the num1:ten
please make sure you enter numbers.Make sure denomimator is non-zero

In [10]:
# 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 numbers.Make sure denomimator is non-zero"
else:
    print "results of the operation is {}".format(result)


please enter the num1:10
please enter the num2:0
please make sure you enter numbers.Make sure denomimator is non-zero

In [11]:
# to narrow down to our exceptions
try:
    num1 = int(raw_input("please enter the num1:"))
    num2 = int(raw_input("please enter the num2:"))
    result = num1/num2
except (ZeroDivisionError,ValueError):
    print "please make sure you enter numbers.Make sure denomimator is non-zero"
else:
    print "results of the operation is {}".format(result)


please enter the num1:10
please enter the num2:0
please make sure you enter numbers.Make sure denomimator is non-zero

In [12]:
#How to handle customized exceptions
try:
    num1 = int(raw_input("please enter the num1:"))
    num2 = int(raw_input("please enter the num2:"))
    result = num1/num2
except ZeroDivisionError:
    print "Make sure your denominator is non-zero."
except ValueError:
    print "please make sure you enter numbers."
else:
    print "results of the operation is {}".format(result)


please enter the num1:10
please enter the num2:0
Make sure your denominator is non-zero.

In [14]:
print "hello world


  File "<ipython-input-14-5ac767fbdae9>", line 1
    print "hello world
                     ^
SyntaxError: EOL while scanning string literal

In [ ]:
# finally
# case I: I enter all valid values .. try..else..finally.
# case II: I enter invalid values.. handled by exceptions .. try..except..finally
# case III: I enter invalid values.. not handled by exceptions..try..finally..exception issue.

In [17]:
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 numbers."
else:
    print "results of the operation is {}".format(result)
finally:
    print "close your resource here."
    # database.close
    # socket.close
    # file.close


please enter the num1:10
please enter the num2:0
All is well.
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-17-765135418bd4> in <module>()
      2     num1 = int(raw_input("please enter the num1:"))
      3     num2 = int(raw_input("please enter the num2:"))
----> 4     result = num1/num2
      5 except ValueError:
      6     print "please make sure you enter numbers."

ZeroDivisionError: integer division or modulo by zero

In [18]:
# raise
raise SyntaxError


  File "<string>", line unknown
SyntaxError

In [19]:
raise SyntaxError,"Buddy!! please clean your glasses."


  File "<string>", line unknown
SyntaxError: Buddy!! please clean your glasses.

In [20]:
# your cannot raise every other word.
# santosh needs to be part of the exception class.
raise santosh


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

NameError: name 'santosh' is not defined

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

In [22]:
raise santosh,"I am back"


---------------------------------------------------------------------------
santosh                                   Traceback (most recent call last)
<ipython-input-22-6f7a09299e2c> in <module>()
----> 1 raise santosh,"I am back"

santosh: I am back

In [ ]: