class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
except ShortInputException, x:
print 'ShortInputException: The input was of length %d, \
was expecting at least %d' % (x.length, x.atleast)
I dont understand the syntax of this line: except ShortInputException, x:
what is x here for ??
and why is it acting as an object ???
waht does this line do ? : Exception.__init__(self)
Thanks
catches an exception of class ShortInputException and binds the instance of the exception object to x.
The more common syntax for this is
which is to be preferred as described in PEP3110. Unless you need to support Python 2.5, you should use the as version.
calls the constructor for the super class, the class that this user defined class derives from.