46.一个例子了解C++中异常处理

版权声明:本博客为记录本人学习过程而开,内容大多从网上学习与整理所得,若侵权请告知! https://blog.csdn.net/Fly_as_tadpole/article/details/83387451

异常处理的栈展开、析构函数和构造函数的异常,异常处理的层次

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;
}

猜你喜欢

转载自blog.csdn.net/Fly_as_tadpole/article/details/83387451