1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <sys/resource.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <iostream>
#include <string>
#include <setjmp.h>
#include <fenv.h>
#include <signal.h>
using namespace std;
#define try_with_signal_handler if (!sigsetjmp(env, 1))
#define catch_with_signal_handler else
static sigjmp_buf env;
void sig_handler(int sig)
{
printf (" Application shall not crash on %d \n", sig);
siglongjmp(env,sig);
}
void show_status_of_fpe()
{
int result_fegetexcept = fegetexcept();
if(result_fegetexcept&FE_INVALID)
printf("FE_INVALID\n");
else if(result_fegetexcept&FE_OVERFLOW)
printf("FE_OVERFLOW\n");
else if(result_fegetexcept&FE_UNDERFLOW)
printf("FE_UNDERFLOW\n");
else if(result_fegetexcept&FE_DIVBYZERO)
printf("FE_DIVBYZERO\n");
else
printf("NO FPE\n");
}
void set_signal_on_fpe()
{
feenableexcept(FE_DIVBYZERO|FE_UNDERFLOW|FE_OVERFLOW|FE_INVALID);
//feenableexcept(FE_INVALID|FE_OVERFLOW); // pour verif si show_status_of_fpe marche
signal(8, &sig_handler);
}
int main( void )
{
float a=0.0;
float b=0.0;
int c=0;
int d=0;
//NO SIGNAL CATCH
/*
try_with_signal_handler
{
printf("-------------------------\n");
printf("Div by zero : ");
a = 3.0 / b;
printf("OK : a = %f\n",a);
printf("OVERFLOW : ");
a = exp(1000);
printf("OK : a = %f\n",a);
printf("UNDERFLOW : ");
a = exp(-1000);
printf("OK : a = %f\n",a);
printf("INVALID : ");
a = sqrt(-1);
printf("OK : a = %f\n",a);
}
catch_with_signal_handler
{
printf("Catch fpe exception ...\n");
}
printf("-------------------------\n");
*/
set_signal_on_fpe();
printf("Activating signal handling\n");
// DIV BY ZERO FLOAT
printf("-------------------------\n");
try_with_signal_handler
{
printf("Div by zero : ");
a = 3 / b;
printf("OK : a = %f\n",a);
}
catch_with_signal_handler
{
printf("Catch fpe exception ...\n");
}
printf("-------------------------\n");
//OVERFLOW
/*
printf("-------------------------\n");
set_signal_on_fpe();
try_with_signal_handler
{
printf("OVERFLOW : ");
a = exp(1000);//attention machine 32bit only
printf("OK : a = %f\n",a);
}
catch_with_signal_handler
{
printf("Catch fpe exception ...\n");
}
printf("-------------------------\n");
*/
// UNDERFLOW
/*
printf("-------------------------\n");
set_signal_on_fpe();
try_with_signal_handler
{
printf("UNDERFLOW : ");
a = exp(-1000);//attention machine 32bit only
printf("OK : a = %f\n",a);
}
catch_with_signal_handler
{
printf("Catch fpe exception ...\n");
}
printf("-------------------------\n");
*/
// INVALID
/*
printf("-------------------------\n");
set_signal_on_fpe();
try_with_signal_handler
{
printf("INVALID : ");
a = sqrt(-1);
printf("OK : a = %f\n",a);
}
catch_with_signal_handler
{
printf("Catch fpe exception ...\n");
}
printf("-------------------------\n");
*/
// DIV BY ZERO INT
/*
printf("-------------------------\n");
try_with_signal_handler
{
printf("Div by zero int : ");
c = 3 / d;
printf("OK : c = %f\n",c);
}
catch_with_signal_handler
{
printf("Catch fpe exception ...\n");
}
printf("-------------------------\n");
*/
return 0;
} |
Partager