1. 程式人生 > >46.一個例子瞭解C++中異常處理

46.一個例子瞭解C++中異常處理

異常處理的棧展開、解構函式和建構函式的異常,異常處理的層次

catch的搜尋匹配 。。。

class wrong :public exception
{
public:
	wrong(const int& i = 0,const string& str = ""):err_no(i),err_str(str){}
	string what()
	{
		cout << "錯誤碼: " << err_no << endl;
		cout << "錯誤script: " << err_str<<endl;
		return err_str;
	}
private:
	int err_no;
	string err_str;
};



void throw_err()
{
	throw wrong(1, "沒魚SB");
}


int main()
{
	try {
		try {

			try {

				throw_err();
			}
			catch (string&w) {

				cout << "shabi" << endl;
			}

		}
		catch (wrong& w) {

			w.what();
		}
	}
	catch (...) {
	
		cout << "沒魚SB2" << endl;
	}
	system("pause");
	return 0;
}