python中有关于exception的疑问,需要创造两种错误的类(LenError和TypeError),怎么解决呢?
class LenError(Exception):
pass
class TypeError(Exception):
pass
def isValidID(IDnum):
try:
if len(str(IDnum))!=4:
raise LenError()
if type(1)!=type(IDnum):
raise TypeError()
return "Valid ID number"
except LenError as e:
return "The length of your ID is not valid"
except TypeError as e:
return "The type of your ID is not valid"
print(isValidID('1234'))
print(isValidID(12345))
print(isValidID(1234))
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!