#!/usr/bin/python
#Filename:raising.py
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)
# 0 ther work can continue as usual here except EOFError:
except EOFError:
print '\n Why did you do an E0F on me?'
except ShortInputException,x:
print'ShortInputException: The input was of length %d,was expecting at least %d'%(x.length,x.atleast)
else:
#Filename:raising.py
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)
# 0 ther work can continue as usual here except EOFError:
except EOFError:
print '\n Why did you do an E0F on me?'
except ShortInputException,x:
print'ShortInputException: The input was of length %d,was expecting at least %d'%(x.length,x.atleast)
else:
print'No exception was raised.'
自定义异常ShortInputEcception,注意init前面是两个小横杆,异常类型EOFError全是英文字母。