2
检查有关的异常处理下面的代码在python为什么在函数中返回抑制引发的异常?
class myException(Exception):
def __str__(self):
return 'this is my exception'
class myException2(Exception):
def __str__(self):
return 'this is my exception 2'
def myfunc():
try:
raise myException2
print('after exception')
except myException:
a = 'exception occur'
print(a)
else:
a = 'exception doesn\'t occur'
print(a)
finally:
a = 'no matter exception occurs or not'
print(a)
return a
然后乳宁MYFUNC()将没有任何异常输出弹出
no matter exception occurs or not
但如果在最后条款“返回”代码评论,输出将捕获未处理的myException2,
no matter exception occurs or not
---------------------------------------------------------------------------
myException2 Traceback (most recent call last)
<ipython-input-140-29dfc9311b33> in <module>()
----> 1 myfunc()
<ipython-input-139-ba35768198b8> in myfunc()
1 def myfunc():
2 try:
----> 3 raise myException2
4 print('after exception')
5 except myException:
myException2: this is my exception 2
为什么返回码对于采集卡非常重要异常?
非常感谢。 – Lansiz