如何在 Python 中捕獲 ArithmeticError 異常?


ArithmeticError 異常是所有數字計算中出現的錯誤的基本類。它是這些內建異常的基本類:OverflowError、ZeroDivisionError、FloatingPointError

我們可以按照以下方式在給定程式碼中捕獲異常

示例

import sys
try:
7/0
except ArithmeticError as e:
print e
print sys.exc_type
print 'This is an example of catching ArithmeticError'

輸出

integer division or modulo by zero
<type 'exceptions.ZeroDivisionError'>
This is an example of catching ArithmeticError

更新時間: 2020-6-12

961 個瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告