In [ ]:
# exception are error handling in programming.

In [1]:
# use case 1:

num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print result


Please enter the num1:10
please enter the num2:2
5

In [2]:
# error1:
num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print result


Please enter the num1:10
please enter the num2:0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-2-6575aff5ee4d> 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 result

ZeroDivisionError: integer division or modulo by zero

In [3]:
# error 2
num1 = int(raw_input("Please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1/num2
print result


Please enter the num1:a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-d190fa52622b> in <module>()
----> 1 num1 = int(raw_input("Please enter the num1:"))
      2 num2 = int(raw_input("please enter the num2:"))
      3 result = num1/num2
      4 print result

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

In [4]:
# what are different exception python handles.

import exceptions as e

In [5]:
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 : code which is mandatory(must).
# except: handles your exceptions.
# else: if the try block is true. what we need to do.
# finally

In [7]:
## resolving errors
## use case 1:
# except is below example is very broad, which mean it covers all exceptions.

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 integers and your denominator is non-zero"
else:
    print result


Please enter the num1:10
please enter the num2:0
please make sure you enter integers and your denominator is non-zero

In [8]:
## use 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 integers and your denominator is non-zero"
else:
    print result


Please enter the num1:a
please make sure you enter integers and your denominator is non-zero

In [9]:
## use 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 integers and your denominator is non-zero"
else:
    print result


Please enter the num1:10
please enter the num2:2
5

In [ ]:
## use the exception handles in code.

In [10]:
## use case 1:

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 integers." 
except ZeroDivisionError:    
    print "Please make sure your denominator is non-zero"
else:
    print result


Please enter the num1:a
please make sure you enter integers.

In [12]:
## use case 2:

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 integers." 
except ZeroDivisionError:    
    print "Please make sure your denominator is non-zero"
else:
    print result


Please enter the num1:10
please enter the num2:0
Please make sure your denominator is non-zero

In [ ]:
## use tuple of exceptions

In [15]:
## here we are not covering all exception.
## we are only dealing with ValueError and ZeroDivisionError

try:
    num1 = int(raw_input("Please enter the num1:"))
    num2 = int(raw_input("please enter the num2:"))
    result = num1/num2
except (ValueError,ZeroDivisionError):
    print "please make sure you enter integers.Please make sure your denominator is non-zero" 
else:
    print result


Please enter the num1:10
please enter the num2:0
please make sure you enter integers.Please make sure your denominator is non-zero

In [ ]:
# try : code which is mandatory(must).
# except: handles your exceptions.
# else: if the try block is true. what we need to do.
# finally:
# case1 : i will enter all valid values .. try.. else.. finally.
# case2 : i will enter an invalid value.. handled by exceptions .. try..except..finally.
# case3 : i will enter invalid values .. not handled by exceptions.. try..finally..bombed out of the program.

In [19]:
## what if we have not handled an exception in our code.
## how do we deal with this?
## finally block make sure you close all your resource in sane way..before bombing out of an program.
## file.close,socket.close,db.close.

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 integers." 
else:
    print result
finally:
    print "we are all good"
    # db.close
    # file.close
    # socket.close


Please enter the num1:10
please enter the num2:0
we are all good
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-19-e18f5e09ed3f> in <module>()
      5     num1 = int(raw_input("Please enter the num1:"))
      6     num2 = int(raw_input("please enter the num2:"))
----> 7     result = num1/num2
      8 except ValueError:
      9     print "please make sure you enter integers."

ZeroDivisionError: integer division or modulo by zero

In [ ]:
## raise keyword.
# All your exception are part of Exception class.
# raise keyword will only work on exception which are part of Exception class

In [20]:
raise SyntaxError


  File "<string>", line unknown
SyntaxError

In [21]:
# comment to the exception.
raise SyntaxError,"Please clean your glasses"


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

In [22]:
raise santosh


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

NameError: name 'santosh' is not defined

In [24]:
## so how to make santosh part of exeption class.
## note: OOP is yet to come do relax.
# we will deal with our own custome exception when we deal with OOP.(classes)


class santosh(Exception):
    pass

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


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

santosh: hey i am back!!!

In [ ]: