Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to catch FloatingPointError Exception in Python?

When the code given below is run, I get an error.

import sys
import math
import fpectl
print 'Control off:', math.exp(700)
fpectl.turnon_sigfpe()
print 'Control on:', math.exp(1000)

How do I catch this error and know its type?


1 Answer
Rajendra Dharmkar

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.

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'>

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.