// Compile me with: // // gcc -v -g -O2 -Wall testexception.cc -o testexception // // ... and I won't work. I output ... // // About to sub2 // About to sub2 // About to sub2 // About to sub2 // CAUGHT but SHOULDN'T HAVE! // // ... instead of ... // // About to sub2 // CAUGHT i 123 // // NOTE that I work fine if you compile me with: // // gcc -v -g -Wall testexception.cc -o testexception // // (without the -O2 option, that is) extern "C" int printf(const char *, ...); class MMMM { public: typedef void (MMMM::*Method)(int i); void sub2(int ii) { throw ii; } void sub1(int ii) { try { try { printf("About to sub2\n"); sub2(ii); } catch (int &i) { printf("CAUGHT i %d\n", i); } } catch (...) { printf("OOPS how did we get here?\n"); } } }; int main(int argc, char *argv[]) { MMMM mmmm; try { MMMM::Method x = (MMMM::Method) &MMMM::sub1; ((&mmmm)->*(x))(123); } catch (...) { printf("CAUGHT but SHOULDN'T HAVE!\n"); } return 0; }