传统的错误处理方法


#include <iostream>
#include <csetjmp>

using namespace std;

class Rainbow{
	public:
		Rainbow(){
			cout<<"Rainbow()"<<endl;
		}
		~Rainbow(){
			cout<<"~Rainbow()"<<endl;
		}
};

jmp_buf kansas;

void oz(){
	Rainbow rb;
	for(int i =0; i<3;i++){
		cout<<"there's no place like home"<<endl;
	}
	longjmp(kansas,47);
}

int main(){
	if(setjmp(kansas) ==0){
		cout<<"tornao, with munchkins .."<<endl;
		oz();
	}
	else{
		cout<<"Anutie Em! "
		    <<"I had the strangest dream..."
		    <<endl;
	}
}

XXXX:XXXXX$ g++ nonloacl.cpp
XXXX:XXXXX$ ./a.out
输出结果如下所示:
tornao, with munchkins ..
Rainbow()
there's no place like home
there's no place like home
there's no place like home
Anutie Em! I had the strangest dream...




关于程序设计基石与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.

你可能感兴趣的:(传统的错误处理方法)