2
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

1
  • Note that, for reasons you can find outlined in the docs, its a bit better to do super(ShortInputException, self).__init__(self) than Exception.__init__(self); but in your code as written, it will work out the same. Commented May 23, 2012 at 7:01

2 Answers 2

7
except ShortInputException, x:

catches an exception of class ShortInputException and binds the instance of the exception object to x.

The more common syntax for this is

except ShortInputException as x

which is to be preferred as described in PEP3110. Unless you need to support Python 2.5, you should use the as version.


Exception.__init__(self)

calls the constructor for the super class, the class that this user defined class derives from.

Sign up to request clarification or add additional context in comments.

4 Comments

Tell me if i am wrong. The flow is like this 1. s = raw_input('Enter something --> ') if len(s) < 3: raise ShortInputException(len(s), 3) 2. except ShortInputException, x: print 'ShortInputException: The input was of length %d, \ was expecting at least %d' % (x.length, x.atleast) where in between the class ShortInputException(Exception): comes ????
This answer is just correct. Being correct is enough to be worth an upvote ;)
You need __ around the init, i.e., __init__. Also, it isn't really the "more common syntax": the as syntax was added in Python 2.6 (IIRC), before that, the except FooType, foo syntax was used. The old syntax is still supported in Python 2, but if you're on Python 2.6, you should prefer the newer syntax. But if you need to support Python 2.5 (poor you) then you'd have to use the old syntax, to be compatible.
@thanatos Thanks. The init was a copy/paste error from original formatting of the question and I just found PEP3110 which describes the change from old comma syntax to the new syntax.
1

waht does this line do ? : Exception.__init__(self)

ShortInputException(Exception) declares your class ShortInputException as sub class of Exception. Exception.__init__(self) calls the constructor of parent class.

except ShortInputException, x:

From the doc:

When an exception occurs, it may have an associated value, also known as the exception’s argument. The presence and type of the argument depend on the exception type.

The except clause may specify a variable after the exception name (or tuple). The variable is bound to an exception instance with the arguments stored in instance.args.

x in your example is the exception object raised.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.