捕获时:
except MyException as e:
print(e) #将打印出异常信息
#自定义异常class LengthRequiredException(Exception):def __init__(self,length,minLength):Exception.__init__(self)self.length = lengthself.minLength = minLength#引发自定义的异常l = [1,2,3,4,5]minLength = 6try:raise LengthRequiredException(len(l),minLength)except IndexError:print("index out of bounds")except LengthRequiredException as e:print("Length not fit :length is %d required %d" %(e.length,e.minLength))else:print("no exception was raised")finally:print("finally will be execute")