python-Exception异常使用

Exception
#自定义异常类 ,MyInputExcp继承Exception异常
class MyInputExcp(Exception):
    def __init__(self, lenght, least):
        self.length = lenght
        self.least = least

try:
    #s = raw_input("python2请输入:
")
    s = input("python3请输入:
")
    if (len(s)<6):
        raise MyInputExcp(len(s),6) #强制异常
except MyInputExcp as x:
    print("发生异常,输入的长度为%d,长度应该大于等于%d"%(x.length,x.least))
else:
    print("无异常")
finally:
    print("不论异常都执行")
原文地址:https://www.cnblogs.com/shuzf/p/11170697.html