Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Catch FloatingPointError Exception ใน Python

FloatingPointError raised เมื่อ floating point operation error และ fpectl เปิดอยู่ (ต้อง compile Python ด้วย --with-fpectl)

ตัวอย่าง

import sys
import math
import fpectl

try:
    print('Control off:', math.exp(700))
    fpectl.turnon_sigfpe()
    print('Control on:', math.exp(1000))
except Exception as e:
    print(e)
    print(sys.exc_type)

ผลลัพธ์

Control off: 1.01423205474e+304
Control on: in math_1