Computer >> Máy Tính >  >> Lập trình >> Python

Làm thế nào để bắt ArithmeticError Exception trong Python?

ArithmeticError Exception là lớp cơ sở cho tất cả các lỗi xảy ra đối với các phép tính số. Nó là lớp cơ sở cho những ngoại lệ tích hợp sẵn như:OverflowError, ZeroDivisionError, FloatingPointError

Chúng tôi có thể bắt ngoại lệ trong mã nhất định như sau

Ví dụ

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

Đầu ra

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