C++ Standard Library C++ STL Library

C++ <cfenv> - fegetexceptflag() Function



The C++ <cfenv> fegetexceptflag() function attempts to store the floating-point exception flags indicated by excepts into the fexcept_t object pointed by flagp. Programs calling this function shall ensure that pragma FENV_ACCESS is enabled for the call.

Syntax

int fegetexceptflag (fexcept_t* flagp, int excepts);

Parameters

flagp Specify pointer to an fexcept_t object where the flags will be stored.
excepts

Specify bitmask listing the floating-point exception flags to get.

MacrosDescription
FE_DIVBYZERO Pole error exception occurred in an earlier floating-point operation.
FE_INEXACT Inexact result exception occurred in an earlier floating-point operation (rounding was necessary to store the result).
FE_INVALID Invalid argument exception occurred (domain error occurred) in an earlier floating-point operation.
FE_OVERFLOW Overflow range error exception occurred in an earlier floating-point operation (result was too large to be representable).
FE_UNDERFLOW Underflow range error exception occurred in an earlier floating-point operation (result was subnormal with a loss of precision).
FE_ALL_EXCEPT Bitwise OR of all supported floating-point exceptions.

Certain library implementations may define additional macro constants in <cfenv> to identify additional floating-point exceptions (with their corresponding macros also beginning with FE_).

Return Value

Returns 0 if the flags are successfully stored. Returns a non-zero value otherwise.

Exception

This function never throws exceptions.

Example:

The example below shows the usage of fegetexceptflag() function.

#include <iostream>
#include <cfenv>
#include <cmath>

#pragma STDC FENV_ACCESS ON

using namespace std;

void FE_Exceptions_Message(void) {
  cout<<"Exceptions raised:";
  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";
  cout<<endl;
}

int main (){
  //creating fexcept_t object
  fexcept_t excepts; 

  //raising FE_DIVBYZERO exception
  feraiseexcept(FE_DIVBYZERO);
  FE_Exceptions_Message();

  //storing current exception flag
  fegetexceptflag(&excepts,FE_ALL_EXCEPT);

  //clear previous exception flag and 
  //create another exception flags
  feclearexcept(FE_ALL_EXCEPT);
  feraiseexcept(FE_INVALID | FE_INEXACT);
  FE_Exceptions_Message();

  //restoring previous exception flags
  fesetexceptflag(&excepts,FE_ALL_EXCEPT);
  FE_Exceptions_Message();

  return 0;
}

The output of the above code will be:

Exceptions raised: FE_DIVBYZERO
Exceptions raised: FE_INEXACT FE_INVALID
Exceptions raised: FE_DIVBYZERO

❮ C++ <cfenv> Library