如何在 Python 中捕获 ArithmeticError Exception?

pythonserver side programmingprogramming更新于 2026/1/30 10:52:17

ArithmeticError Exception 是数值计算中发生的所有错误的基类。它是以下内置异常的基类: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

相关文章


有用资源