Exception Classes : Error Exception « Statement « Python Tutorial






class RangeException(Exception) :
    def __init__(self, low, high) :
        self.low = low
        self.high = high
    def __str__(self) :
        s = "Expected value in range "+str(self.low)
        s = s + " to "
        s = s + str(self.high)
        return s

def CheckValue( inV ) :
    if ( inV < 0 or inV > 10 ) :
        raise RangeException(0,10)
    return True

CheckValue(12)

import sys

try :
       CheckValue(12)
except Exception:
       print sys.exc_info()

try :
      CheckValue(12)
except RangeException, re:
      print "You didn't enter a value between: "+str(re.low)+", and "+str(re.high)








3.9.Error Exception
3.9.1.Catching Exceptions with try..except
3.9.2.ZeroDivisionError: division by any numeric zero
3.9.3.IndexError: request for an out-of-range index for sequence
3.9.4.KeyError: request for a non-existent dictionary key
3.9.5.AssertionError
3.9.6.IOError: input/output error
3.9.7.AttributeError: attempt to access an unknown object attribute
3.9.8.try-except Statement
3.9.9.try Statement with Multiple excepts
3.9.10.except Statement with Multiple Exceptions
3.9.11.replace our single error string with the string representation of the exception argument.
3.9.12.Demonstrating a programmer-defined exception class.
3.9.13.Exceptions Can Be Classes
3.9.14.Exception Classes
3.9.15.String-Based Exceptions
3.9.16.Working with the Exception Information