建議更好的處理 Python 異常的方法


我們可以使用 finally 子句來清理是否丟擲異常

try:
  #some code here
except:
  handle_exception()
finally:
  do_cleanup()

如果在異常事件中進行清理,我們可以按如下方式編碼

should_cleanup = True
try:
  #some code here
  should_cleanup = False
except:
  handle_exception()
finally:
  if should_cleanup():
    do_cleanup()

更新時間:2019 年 9 月 27 日

137 次瀏覽

開啟 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.