永远不要让用户看见大黄页
def foo(x):
x = x + 1
print (x)
try:
foo(rf)
foo(2)
except Exception as a: # except:
print(a)
else:
print("It's OK")
finally:
print("This is must")
class MyException(Exception): #从Exception继承错误,写自己的错误类
def __init__(self,msg):
self.error = msg
def __str__(self, *args, **kwgs): #__str__可以直接返回值
return self.error
a = MyException('自定义错误信息')
print('{}'.format(a)) #通过__str__用print直接打出错误信息
raise MyException('手动触发错误') #主动触发异常