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