Python 异常捕获,自定义异常
2018-11-08 本文已影响44人
爱吃猫的老虎
class MyException(TypeError): # 自定义异常,继承TypeError
def __index__(self, arg):
self.args = arg
def addTwoNumber(a,b):
try:
if isinstance(a,str) or isinstance(a,str):
raise MyException('我需要的是数值类型,而你给我的却是字符串')
else:
print(a + b)
except MyException as err:
print(err)
except Exception as err:
print(err)
else:
print('我是else')
finally:
print('我是finally')
addTwoNumber(13,12)
addTwoNumber('a',12)