class Test(object):
def init(self,switch):
self.switch = switch
def calc(self,a,b):
try:
return print(a/b)
except Exception as result:
if self.switch:
print(“捕获异常成功,结果为:%s”%result)
else:
print(“抛出异常”)
raise
else:
print(“没有异常情况”)
a = Test(True)#定义对象
a.calc(1,6)
a.switch = False
a.calc(4.0)