建議使用更簡潔的方式來處理 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()
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP