Advertisement

g++ and exceptions

Started by July 17, 2003 11:31 AM
2 comments, last by joanusdmentia 21 years, 2 months ago
Hi, I''ve just noticed something odd with exception handling under g++. For the following code after throw Exception() execution enters the catch(...) of func1() but after rethrowing it doesn''t enter catch(...) of func2(). That is, rethrowing doesn''t seem to work properly, I just get a segfault. The rethrow seems to be happening but it just isn''t getting caught. The same code under VC++7 works as expected (the rethrow is caught properly). The following isn''t EXACTLY what I have but the basic operation is the same. Any ideas as to what would cause this?

void func1()
{
    try
    {
        // Do stuff

        if(failed) throw Exception();
    }
    catch(...)
    {
        // Clean up

        throw;
    }
}

void func2()
{
    try
    {
        func1();
    }
    catch(...)
    {
        // Handle exception

    }
}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
This code:
#include <iostream>void func1() {	try {		throw int(5);	} catch(...) {		std::cout << "func1: catch, rethrow" << std::endl;		throw;	}}void func2() {	try {		std::cout << "func2: calling func1" << std::endl;		func1();	} catch(...) {		// Handle exception		std::cout << "func2: catch" << std::endl;	}}int main() {	std::cout << "main: calling func2" << std::endl;	func2();}

Produces this output under GCC 2.95, 3.2, and 3.3:
main: calling func2func2: calling func1func1: catch, rethrowfunc2: catch

That''s what I''d expect. You should probably produce a complete example (try my code?) and the version of GCC you''re using.

Advertisement
If its segfaulting then its something wrong with your code.

Unhandled exceptions usually cause the process to abort, rather than segfault.

unless you changed the unhandled exception handler....
Well, despite all my efforts at the time to fix it to no avail, a little development in windows (on the same project, it''s windows & linux) and coming back to linux and it''s mysteriously working!!!

It obviously was a stuff-up on my part. I''d made quite a few changes so I''m not really sure what would have done it, but I''m fairly sure it was in a destructor getting called between the rethrow and the 2nd catch.

Thanks anyway!

"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement