This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Nonterminating exceptions with longjump?
- From: Gabor Greif <gabor at mac dot com>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 2 Feb 2003 23:43:17 +0100
- Subject: Nonterminating exceptions with longjump?
I have a crazy idea for a long time but I did not dare to
articulate it till now. I know it is not standard-conformant,
but I just want to know whether it would work with g++.
I intend to simulate non-terminating exceptions on top of the
g++ exception handling machinery (sjlj-based or
table based, whatever). The call stack won't
contain destructors, just exception handlers.
consider this (untested, pseudocode):
struct non_terminate
{
jmp_buf resume;
};
// demonstrate throw
//
void bar(void)
{
non_terminate exc;
if (setjmp(exc.resume))
{
// do I need to call some library
// function to end the throw?
// __exception_complete(); // or such...
// report it
cout << "resuming execution" << endl;
cout << "throwing again" << endl;
throw exc;
}
else
{
cout << "throwing exception" << endl;
throw exc;
}
}
// demonstrate handler
//
void foo(void)
{
try {
bar();
} catch (non_terminate& resumer)
{
cout << "handling exception" << endl;
throw;
}
}
// main routine
void thread_entry(void)
{
try {
foo();
} catch (non_terminate& resumer)
{
cout << "resuming to thrower" << endl;
longjmp(resumer.resume,1);
} catch (...)
{
cout << "huh?" << endl;
}
}
I understand that in order for this to work, the stack
unwinding must happen after all handlers are done
and there must be a callable function like
__exception_complete that "unhappens" the exception
but does not cut the stack. This is needed in order to
avoid "terminate" because of an exception thrown
while unwinding.
I am interested to know whether there is any chance that
this scheme does work (and keeps working in forseeable
future) with g++.
Thanks for your patience.
Gabor