如何在使用 Python 的“except 子句”時不丟擲任何異常?


如果我們定義不丟擲任何異常的 except 子句,它可以處理所有型別的異常。但是,既不是良好的編碼實踐,也不建議這麼做。

範例

try:
print 'foo'+'qux'+ 7
except:
print' There is error'

輸出

獲得輸出

There is error

此型別的 Python try-except 塊可以處理所有型別的異常,但對程式設計師找出發生哪種型別的異常無益。

更新日期: 12-Feb-2020

2K+ 次瀏覽

事業起航

完成課程獲得認證

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