⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_exception_handling_5_b.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//****************************************************
// 演示抛出并处理异常类
//****************************************************

# include <iostream>
# include <string>

using namespace std;

class MyException
{
public:
	string Display()
	{
		return " : Not Positive";
	}
};

int main()
{
	cout<<"Begin of main()"<<endl;

	int i;

	try //测试异常
	{
		cout<<"Begin of try block"<<endl;
		cout<<"Input a positive number: ";
		cin>>i;
		if(i<0)
		{
//			throw MyException;   //Error: 可以抛出类对象,但不可以抛出类型
			throw MyException(); //创建并抛出无名对象
		}
		cout<<"End of try block"<<endl;
	}

	catch(MyException me) //捕获类对象
	{
		cout<<"Begin of catch block"<<endl;
		cout<<i<<me.Display()<<endl;
		cout<<"End of catch block"<<endl;
	}
	
	cout<<"Begin of main()"<<endl;

	return 0;
}

/*
Begin of main()
Begin of try block
Input a positive number: -1
Begin of catch block
-1 : Not Positive
End of catch block
Begin of main()
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -