C++ feraiseexcept()

The feraiseexcept() function in C++ attempts to raise all the floating point exceptions specified by the argument.

The feraiseexcept() function is defined in <cfenv> header file.

feraiseexcept() prototype

int feraiseexcept(int excepts);

The floating point exceptions to be raised are listed in the argument excepts.

Also, you should enable FENV_ACCESS, which will give your program to access the Floating point environment to test the exceptions raised.


feraiseexcept() Parameters

  • excepts: Bitmask listing of exception flags to raise.
Bitmask accepted macros
Macro Type Description
FE_DIVBYZERO Pole error Division by zero
FE_INEXACT Inexact Not exact results such as (1.0/3.0)
FE_INVALID Domain error At least one arguments used is a value for which the function is not defined
FE_OVERFLOW Overflow range error Result is too large in magnitude to be represented by the return type
FE_UNDERFLOW Underflow range error Result is too small in magnitude to be represented by the return type
FE_ALL_EXCEPT All exceptions All exceptions supported by the implementation

feraiseexcept() Return value

  • The feraiseexcept() function returns zero value if all the exceptions specified by excepts are raised.
  • Otherwise it returns a nonzero value.

Example: How feraiseexcept() function works

#include <iostream>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
using namespace std;

int main()
{
	int retVal;
	feclearexcept(FE_ALL_EXCEPT);

	retVal = feraiseexcept(FE_OVERFLOW | FE_INVALID);
	if (retVal == 0)
		cout << "Successfully raised FE_OVERFLOW and FE_INVALID" << endl;
	else
		cout << "Raising FE_OVERFLOW and FE_INVALID failed" << endl;

	return 0;
}

When you run the program, the output will be:

Successfully raised FE_OVERFLOW and FE_INVALID