python Exception Handling

Exception Handling

  • Syntax Error

    • Grammatical error
    • IDE checks syntax error automatically
    • Becasue python’s grammar is simpler relatively, error occur rate is smaller and it is easy to delete errors
  • Exception

    • the cases having no syntax errors, the program can’t progress
    • Occuring exception, the program ends
    1. NameError (Usiung undefined variable)

      1
      4 + boo*3
    2. ZeroDivisionError (Dividing by 0)

      1
      2
      3
      a = 10
      b = 0
      c = a / b
    3. TypeError (ex: String + Number type)

      1
      '2' + 2
    4. IndexError (Using index over the range of index)

      1
      2
      l = [1,2]
      print l[2]
    5. KeyError (Searching dictionary by unregisted key)

      1
      2
      d = {"a":1, "b":2}
      print d['c']
    6. IOError (Opening not existing file)

      1
      a = open('aaa.txt')

How to dispose of exceptions

  1. Using try,except,else,finally statements

    • By expecting the situations making exceptions, we can control the whole code’s flow
    • try,except,else,finally

      • structure
        1
        2
        3
        4
        5
        6
        7
        8
        try:
        (possible to make errors) usual statements
        except Exception:
        the statements working when error occurs
        else:
        the statements working when error doesn't occur
        finally:
        the statements working anyway no matter error

  • Doing exception handling, when error occurs the program doesn’t stop

    1
    2
    3
    4
    5
    a = 0
    try:
    print 1.0/a
    except ZeroDivisionError:
    print 'zero division error!!!'
  • msgvariable: the message that the person who defined ZeroDivisionError gives
    ex: float division by zero

    1
    2
    3
    4
    5
    6
    7
    8
    def division():
    for n in range(0, 5):
    try:
    print 10.0 / n
    except ZeroDivisionError, msg
    print msg

    division()
  • It can handle the error from the called function in trystatement indirectly

    1
    2
    3
    4
    try:
    spam()
    except NameError, msg:
    print 'Error -', msg

    , can be replaced with as

    1
    2
    3
    4
    5
    # same code with upper one
    try:
    spam()
    except NameError as msg:
    print 'Error -', msg
  • After except, not evincing any exceptions, it takes care of all exceptions

    1
    2
    3
    4
    5
    try:
    spam()
    print 1.0/0.0
    except:
    print 'Error'
  • About many exceptions, they can be taked care by each exceptstatement

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    b = 0.0
    name = 'aaa.txt'
    try:
    print 1.0 / b
    spam()
    f = open(name, 'r')
    '2' + 2
    except NameError:
    print 'NameError !!!'
    except ZeroDivisionError:
    print 'ZeroDivisionError !!!'
    excpet (TypeError, IOError):
    print 'TypeError or IOERror !!!'
    else:
    print 'No Exception'
    finally
    print 'Exit !!!'

Catching All exception in same kind

  • By using the hierarchy of Inheritance of exception class, It can take care of many exceptions by once
  • For example, there are FloatingPointError, OverflowError, ZeroDivisionError as sub-class of ArithmeticError so, about sub-class exceptions, ArithmeticError can take care of them

  • When an exception is catched by specific except, that one is not catched in other except

    1
    2
    3
    4
    5
    6
    7
    8
    9
    def dosomething():
    a = 1/0

    try:
    dosomething()
    except ZeroDivisionError:
    print "ZeroDivisionError occured"
    except ArithmeticError:
    print "ArithmeticError occured"
Comments