The feholdexcept() function is defined in <cfenv> header file.
feholdexcept() prototype
int feholdexcept( fenv_t* envp );
The feholdexcept() function saves the current floating point environment to the object pointed by envp as done by fegetenv() and the clears all the floating point status flags.
Finally it installs the non-stop mode so that the future floating-point exceptions will not interrupt execution, until the floating-point environment is restored by calling feupdateenv or fesetenv.
feholdexcept() Parameters
- envp: Pointer to an object of type fenv_t that stores the status of the floating point environment.
feholdexcept() Return value
- On success, the feholdexcept() function returns 0.
- On failure, it returns nonzero.
Example: How feholdexcept() function works?
#include <iostream>
#include <cmath>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
using namespace std;
void print_exceptions()
{
cout << "Raised exceptions: ";
if(fetestexcept(FE_ALL_EXCEPT))
{
if(fetestexcept(FE_DIVBYZERO))
cout << "FE_DIVBYZERO ";
if(fetestexcept(FE_INEXACT))
cout << "FE_INEXACT ";
if(fetestexcept(FE_INVALID))
cout << "FE_INVALID ";
if(fetestexcept(FE_OVERFLOW))
cout << "FE_OVERFLOW ";
if(fetestexcept(FE_UNDERFLOW))
cout << "FE_UNDERFLOW ";
}
else
cout << "None";
cout << endl;
}
int main(void)
{
fenv_t envp;
/* raise certain exceptions */
feraiseexcept(FE_INVALID|FE_DIVBYZERO);
print_exceptions();
/* saves and clears current exceptions */
feholdexcept(&envp);
print_exceptions();
/* restores saved exceptions */
feupdateenv(&envp);
print_exceptions();
return 0;
}
When you run the program, the output will be:
Raised exceptions: FE_DIVBYZERO FE_INVALID Raised exceptions: None Raised exceptions: FE_DIVBYZERO FE_INVALID