myexcept.cpp

来自「Visual C++高级编程及其项目应用开发(含源代码)」· C++ 代码 · 共 40 行

CPP
40
字号
#include <iostream>
#include <string>
using namespace std;

void push(string) throw(string);
void pop(string) throw(string);

void main()
{
	string enterStr;
	cout<<"Please enter a string:"<<endl;
	cin>>enterStr;

	try
	{
		if(enterStr=="push")
		{
			push("Push Error!");		
		}
		else
		{
			pop("Pop Error!");
		}
	}
	catch(string &str)
	{
		cerr<<"Exception Error:  "<<str<<endl;
	}
}

void push(string str) throw(string)
{
	cout<<"Now is Push!"<<endl;
	throw string(str);
}
void pop(string str) throw(string)
{
	cout<<"Now is Pop!"<<endl;
	throw string(str);
}

⌨️ 快捷键说明

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