[cpp]
//author:DriverMonkey
//date:1/24/2013
//copyright: no copyright
//mail:
[email protected]
//enviromen:Ubuntu
#include <iostream>
#include <setjmp.h>
using namespace std;
static jmp_buf jmpbuffer;
static void catch_exception(void);
static void raise_exception_1(void);
static void raise_exception_2(void);
int main(int argc, char *argv[])
{
cout<<"main()++"<<endl;
catch_exception();
raise_exception_1();
raise_exception_2();
cout<<"main()--"<<endl;
return 0;
}
static void catch_exception(void)
{
cout<<"catch_exception()++"<<endl;
setjmp(jmpbuffer);
cout<<"catch_exception()--"<<endl;
}
static void raise_exception_1(void)
{
cout<<"raise_exception_1()++"<<endl;
longjmp(jmpbuffer, 1);
cout<<"raise_exception_1()--"<<endl;
}
static void raise_exception_2(void)
{
cout<<"raise_exception_2()++"<<endl;
longjmp(jmpbuffer, 2);
cout<<"raise_exception_2()--"<<endl;
}
Terminal Output:
main()++
catch_exception()++
catch_exception()--
raise_exception_1()++
catch_exception()--
raise_exception_2()++
catch_exception()--
main()--