vgokul
October 7th, 2009, 03:36 PM
Hello all,
I am new to exception handling and I have some problems. Can you please help?
I have a legacy code that has divide by zero error at the start for the first few function calls but has no functional effect if they are ignored (I have checked this in Windows with SEH). Now I need the same in the Linux environment and I tried in a smaller test code that is representative of my original code - please see the code below. The exception is ignored the first time but in the second call float point exception occurs. Can you please let me know how to handle this? Is this the correct way to handle this?
#include<stdio.h>
#include<signal.h>
#include<setjmp.h>
void test();
jmp_buf sjbuf;/*Buffer to store stack position*/
void handler(int signum)
{
if(signum == SIGFPE)
printf("Handler called - Signal error is SIGFPE\n");
signal(SIGFPE, handler);/*Refresh the handle*/
longjmp(sjbuf,1); /* return to saved state */
return;
}
int main()
{
signal(SIGFPE, handler);
//signal(SIGFPE, SIG_IGN);
printf("Inside main\n");
if(setjmp(sjbuf)==0) /* save current stack position */
{
printf("Saved Stack\n");
test();
}
printf("Continuing main\n");
if(setjmp(sjbuf)==0) /* save current stack position */
{
printf("Saved Stack\n");
test();
}
getchar();
return 1;
}
void test()
{
int x;
int y;
printf("Inside test\n");
y= 0;
x = 1/y;
}
Regards
V.Gokul
I am new to exception handling and I have some problems. Can you please help?
I have a legacy code that has divide by zero error at the start for the first few function calls but has no functional effect if they are ignored (I have checked this in Windows with SEH). Now I need the same in the Linux environment and I tried in a smaller test code that is representative of my original code - please see the code below. The exception is ignored the first time but in the second call float point exception occurs. Can you please let me know how to handle this? Is this the correct way to handle this?
#include<stdio.h>
#include<signal.h>
#include<setjmp.h>
void test();
jmp_buf sjbuf;/*Buffer to store stack position*/
void handler(int signum)
{
if(signum == SIGFPE)
printf("Handler called - Signal error is SIGFPE\n");
signal(SIGFPE, handler);/*Refresh the handle*/
longjmp(sjbuf,1); /* return to saved state */
return;
}
int main()
{
signal(SIGFPE, handler);
//signal(SIGFPE, SIG_IGN);
printf("Inside main\n");
if(setjmp(sjbuf)==0) /* save current stack position */
{
printf("Saved Stack\n");
test();
}
printf("Continuing main\n");
if(setjmp(sjbuf)==0) /* save current stack position */
{
printf("Saved Stack\n");
test();
}
getchar();
return 1;
}
void test()
{
int x;
int y;
printf("Inside test\n");
y= 0;
x = 1/y;
}
Regards
V.Gokul