here is my code:
class longInputException(Exception):
def __init__(self, length, max):
Exception.__init__(self)
self.length = len(length)
self.max = max
try:
max = 3
s = raw_input('Enter something-->')
if len(s) > max:
raise longInputException(s, max)
except longInputException, x:
print 'longInputException: the input was of length %d, \
was expecting less than or equal to %d' % (x.length, x.max)
else:
print 'No exception was raised.'
What I don’t understand is why x is used in the longInputException‘s except statement. why not just used self in the substitution tuple?
selfis the name of the current object within__init__()method (because you have providedselfas the first argument in__init__()‘s definition), it is not accessible outside of it.Optionally you can do something like that (although this is not something you should do, as this may confuse people about which variable is which):
Does it answer your question?
You can learn more about that by reading about closures and namespaces in Python.