Open In App

fegetexceptflag() function in C/C++

Last Updated : 25 Sep, 2018
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
The fegetexceptflag() function in C/C++ is specified in header file fenv.h and gets floating point exception flags. This function store the raised exception in the point specified by flagp . Syntax:
int fegetexceptflag(fexcept_t* flagp, int excepts)
Parameters: The function accepts two mandatory parameters which are described below:
  • flagp : signifies the pointer to a fexcept_t object where the representation is stored.
  • excepts : signifies the bitmask value.
Macro --> Description:
  1. FE_DIVBYZERO --> Pole error: Division by zero.
  2. FE_INEXACT --> Inexact: The result is not exact.
  3. FE_INVALID --> Domain error: At least one of the arguments is a value for which the function is not defined.
  4. FE_OVERFLOW --> Overflow range error: The result is too large.
  5. FE_UNDERFLOW --> Underflow range error: The result is too small.
  6. FE_ALL_EXCEPT --> All exceptions.
Return value : The function returns two value as below:
  • Zero: on success.
  • Non-zero: on failure
Below programs illustrate the above function: Program 1 : CPP
// C++ program to illustrate
// fegetexceptflag() function

#include <bits/stdc++.h>
using namespace std;

int main()
{
    // bitmask value
    fexcept_t excepts;

    // divided by zero exception
    feraiseexcept(FE_DIVBYZERO);

    // current state is saved
    fegetexceptflag(&excepts, FE_ALL_EXCEPT);
    cout << "Exception raised -> \n";

    // print the exception occurred
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO \n";
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT \n";
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID \n";
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW \n";
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW \n";
        if (fetestexcept(FE_ALL_EXCEPT))
            cout << "FE_ALL_EXCEPT \n";
    }
    else
        cout << "None";

    return 0;
}
Output:
Exception raised -> 
FE_DIVBYZERO 
FE_ALL_EXCEPT
Program 2 : CPP
// C++ program to illustrate
// fegetexceptflag() function

#include <bits/stdc++.h>
using namespace std;

int main()
{
    // bitmask value
    fexcept_t excepts;

    // raised exception
    feraiseexcept(FE_ALL_EXCEPT);

    // current state is saved
    fegetexceptflag(&excepts, FE_ALL_EXCEPT);
    cout << "Exception raised -> \n";

    // print the exception occurred
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO \n";
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT \n";
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID \n";
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW \n";
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW \n";
        if (fetestexcept(FE_ALL_EXCEPT))
            cout << "FE_ALL_EXCEPT \n";
    }
    else
        cout << "None";

    return 0;
}
Output:
Exception raised -> 
FE_DIVBYZERO 
FE_INEXACT 
FE_INVALID 
FE_OVERFLOW 
FE_UNDERFLOW 
FE_ALL_EXCEPT

Article Tags :
Practice Tags :

Similar Reads