C++ 处理除法异常

#include<iostream>
using namespace std;

int divide(int x, int y)
{
	if (y == 0)//将类型由int换为double后这里检查不出,会在x/y处发生异常,如何解决?
		throw x;
	return x / y;
}

int main()
{
	try
	{
		cout << "5/2 = " <<divide(5, 2) << endl;
		cout << "4/0 = " << divide(4, 0) << endl;
	}
	catch (int e)//或者catch(...)用于接受任何异常,但是如何接受参数e是一个问题
	{
		cout << e << " can't be divided by zero!" << endl;
	}
	system("pause");
	return 0;
}
上一篇:Divide Two Integers


下一篇:BigInteger & BigDecimal