// Showcases the fact that an unhandled local exception ends up returning control // to last resumer rather than first (section 4.5 of course notes). #include #include #include #include using namespace std; _Event ev {}; _Coroutine X { X * partner; string name; static int id; int myid; void main(); public: X(X *partner); void next() { cout << uThisCoroutine().getName() << " before resume" << endl; resume(); cout << uThisCoroutine().getName() << " after resume" << endl; } }; int X::id = 0; X::X(X *partner) : partner( partner ), myid( id ) { id += 1; resume();} void X::main() { ostringstream convert; convert << myid; name = convert.str(); setName( name.c_str() ); suspend(); cout << getName() << " starting" << endl; try { if ( partner != NULL ) partner->next(); else suspend(); cout << getName() << " continuing" << endl; if ( myid == 1 ) { cout << getName() << " throwing exception "<< endl; _Resume ev(); cout << getName() << " after throwing exception "<< endl; } // if } _CatchResume( uBaseCoroutine::UnhandledException) { cout << getName() << " caught unhandled exception" << endl; } // catch cout << getName() << " terminating" << endl; } // X::main int main() { string name = "program main"; uThisCoroutine().setName( name.c_str() ); X x0( NULL ); X x1( &x0 ); X x2( &x1 ); x2.next(); cout << getName() << " caught unhandled exception" << endl; } // main