您当前的位置: 首页 >  Python

59 python - 自定义的异常

杨林伟 发布时间:2019-08-17 10:34:24 ,浏览量:1

你可以用raise语句来引发一个异常。异常/错误对象必须有一个名字,且它们应是Error或Exception类的子类

下面是一个引发异常的例子:

class ShortInputException(Exception):
    '''自定义的异常类'''
    def __init__(self, length, atleast):
        #super().__init__()
        self.length = length
        self.atleast = atleast

def main():
    try:
        s = input('请输入 --> ')
        if len(s)             
关注
打赏
1688896170
查看更多评论
0.0699s