這裏是我的代碼:引發異常問題
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.'
我不明白的是爲什麼x
在longInputException
的except
語句中使用。爲什麼不直接在替換元組中使用self
?