1 class ShortInputException(Exception): 2 def __init__(self, length, atleast): 3 Exception.__init__(self) 4 self.length = length 5 self.atleast = atleast 6 7 try: 8 s = raw_input('Enter something --> ') 9 10 if len(s) < 3 :11 raise ShortInputException(len(s), 3)12 except EOFError:13 print '\nWhy did you do an EOF on me?'14 except ShortInputException, x:15 print 'ShortInputException: The input was of length %d, \16 was expecting at least %d' % (x.length, x.atleast)17 else:18 print 'No exception was raised.'
x就是 ShortInputException的对象 这是python 2.6之前的语法 2.6之后建议使用 except ShortInputException as x: