参考资料:https://blog.csdn.net/hunyxv/article/details/52737339
作用:判断一个程序的错误条件,声明布尔值为真的判定,即raise-if-not,返回值为假就会触发异常
下为一个自定异方式
class ShortInputException(Exception):
def __init__(self,length,atleast):
self.length=length
self.atleast=atleast
def main():
try:
s=input('请输入')
if len(s)<3
raise ShortInputException(len(s),3)
except ShortInputException as result:
print(“输入长度至少为”,result.length)
else:
print('没有异常发生')
main()