6.5.11 feraiseexcept Function
Raises the supported floating-point exceptions represented by its argument.
Include
<fenv.h>
Prototype
void feraiseexcept(int excepts);
Argument
excepts
- a value being the bitwise OR of one or more floating-point exception macros, representing the exceptions to raise
Remarks
The order in which these floating-point exceptions are raised is unspecified, except if the argument represents "overflow" and "inexact", or "underflow" and "inexact", in which case "overflow" or "underflow" is raised before "inexact".
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <fenv.h>
#include <stdio.h>
#include <math.h>
void showExceptions(void)
{
printf("Exceptions currently raised: ");
if(fetestexcept(FE_DIVBYZERO))
printf("FE_DIVBYZERO ");
if(fetestexcept(FE_INEXACT))
printf("FE_INEXACT ");
if(fetestexcept(FE_INVALID))
printf("FE_INVALID ");
if(fetestexcept(FE_OVERFLOW))
printf("FE_OVERFLOW ");
if(fetestexcept(FE_UNDERFLOW))
printf("FE_UNDERFLOW ");
printf("\n");
}
volatile double result = 0.0;
int main(void)
{
fexcept_t excepts;
result = 1.0 / result; // raise FE_DIVBYZERO
showExceptions();
fegetexceptflag(&excepts, FE_ALL_EXCEPT); // save state
feraiseexcept(FE_INVALID|FE_OVERFLOW); // raise exceptions without operation
showExceptions();
fesetexceptflag(&excepts, FE_ALL_EXCEPT); // restore state
showExceptions();
}
Example Output
Exceptions currently raised: FE_DIVBYZERO
Exceptions currently raised: FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW
Exceptions currently raised: FE_DIVBYZERO