How to catch FloatingPointError Exception in Python?


FloatingPointError is raised by floating point operations that result in errors, when floating point exception control (fpectl) is turned on. Enabling fpectl requires an interpreter compiled with the --with-fpectl flag.

The given code is rewritten as follows to handle the exception and find its type.

Example

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

Output

Control off: 1.01423205474e+304
Control on: in math_1
<type 'exceptions.FloatingPointError'>

Updated on: 12-Feb-2020

952 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements