try catch throw c++的异常处理机制的简单程序

/*
test2
try catch throw c++的异常处理机制的简单程序
处理除数遇到0的例子
*/



#include<iostream>
#include <iomanip>  




using namespace std;


namespace Test2 
{
	class test
	{
	public:
		double fuc(double &, double &);






	};
	double test::fuc(double &x, double &y)
	{
		if (y == 0)
		{
			throw "There is a quetion";//---引发异常  
		}
		double c;
		c = x / y;
		return c;
	}


}


int main()
{
	
	using namespace Test2;
	test test1;
	double a, b;
	while (true)
	{
		cout << "请输入除数和被除数:\n";
		cin >> a >> b;
		try//-----------try里面是可能引发异常代码块 
		{
			cout << setprecision(3)<<test1.fuc(a, b) << endl;
		}
		catch (const char * str)//---接收异常,处理异常 
		{
			cout << str << endl;
			break;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/zhangxiafll/article/details/79803672