Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách catch ArithmeticError Exception trong Python?

ArithmeticError Exception là base class cho tất cả errors xảy ra cho numeric calculations. Nó là base class cho những built-in exceptions như: OverflowError, ZeroDivisionError, FloatingPointError.

Chúng ta có thể catch exception trong code cho trước như sau:

Ví dụ

import sys
try:
    7/0
except ArithmeticError as e:
    print(e)
    print(sys.exc_type)
    print('Đây là ví dụ catching ArithmeticError')

Kết quả

integer division or modulo by zero
<type 'exceptions.ZeroDivisionError'>
Đây là ví dụ catching ArithmeticError