如何在 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

更新時間:12-Jun-2020

969 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.