如何自定义异常
首先自定义异常必须继承Exception
#定义一个小于零就会抛出异常的类 ,必须继承Exception
class LessZero(Exception):
def __init__(self, msg, error_code):
self.msg = msg
self.ec = error_code
def __str__(self):
return self.msg + str(self.ec)+ ' 学废了嘛 '
#测试一下
def set_height(h):
if h <= 0 :
raise LessZero("老铁身高不能小于零啊, 错误", 110011)
else:
print("给您的身高设置为:", h)
set_height(-1)