g++ & non-call-exceptions & sigsetjmp !

Devrat Mittal u02113@cs.unipune.ernet.in
Wed Mar 24 13:59:00 GMT 2004


Hello ppl,
	Plz direct me to appropriate g++ mailing list if i got it all 
wrong.
	I've got this small code i made while thinking about c++ 
exceptions, java exceptions & signals. I am able to throw an exception 
from inside a signal handler & its properly caught inside the main catch 
block.

	 However if I remove the sigsetjmp call the thrown exception is
not caught in the main block but lands up with the default handler which 
terminates the process.

/* ---------------------------------------------------------------------*/
/* This program only works properly if you call g++ with 
 the option -fnon-call-exceptions, otherwise it causes
 the exception thrown from inside the signal handler not to be caught
 & this unhandled exception then makes its way to the default exception
 handler which then just aborts & dumps core.

*/

#include <stdexcept>
#include <signal.h>
#include <setjmp.h>
int savesigs;
jmp_buf env;
stack_t ss;
int a; int b; int c;

void sig_handler(int signum, siginfo_t* info, void* arg) {

  printf("Inside the signal Handler.\n"); 
  throw "Floating point exception\n"; 
  return;			/* Unreachable code. */
}

int main(void) {
  struct sigaction act;

  act.sa_sigaction = sig_handler;
  act.sa_flags = SA_ONSTACK | SA_SIGINFO;
  
  if (sigaction(SIGFPE, &act, NULL) != 0) return -1;

  try {
      /*remove this sigsetjmp block & the program behaviour changes.why?*/
      if (sigsetjmp(env, savesigs) !=0) {
	throw "fault thrown in user context(as opposed to signal contxt!";
      }
    a = 12; b =0;
    c = a/b;/* This is to be resumed */
    printf("\nThe value of c is: %d", c); /* Wont be called coz of excep */
  }
  catch(const char* msg) {
    printf("\nException caught.");
    printf("%s", msg);
    printf("\n");
    b=2;		
    /* Resume the exception causing code here. (Is that possible?) 
     goto restart_point where restart_point is a label inside the try block
     would result in a compile error.*/
  }
  printf("\nThe value of a is: %d", a);  
  printf("\nThe value of b is: %d", b);
  printf("\nThe value of c is: %d", c);
  return 0;
}
	
/*-------------------------------------------------------------------*/

..........Here is the output:

/*-----------------------------------------*/
Inside the signal Handler.

Exception caught.Floating point exception


The value of a is: 12
The value of b is: 2
The value of c is: 0

/*----------------------------*/
	
........& here is the output with the sigsetjmp call block 
entirely commented out.

/*-------------------------------*/
Inside the signal Handler.
Aborted (core dumped)
/*-------------------------*/
I dont have the slightest idea how is this happening.
Can someone plz comment on this. 
Thanks,

-- 
Devrat Mittal
Department of Computer Science,
University of Pune.
M.tech 2nd year
Roll no. 2002301





More information about the Gcc-help mailing list