#include <iostream>
#include <cstdlib>
#include <cstring>
#include <windows.h>
using namespace std;
double divv(int x, int y)
{
try
{
if (y==0)
{
throw 1;//抛出
}
else if (x==0)
{
throw 2;
}
return x*1.0 / y;
}
catch (int code)
{
if (code==1)
{
cout << "错误" << endl;
}else if (code==2)
{
cout << "除数为0" << endl;
}
return -1.0;
}
}
double divvstr(int x,int y)
{
try
{
if (y == 0)
{
throw "str你输入的被除数为0";//抛出
}
else if (x == 0)
{
throw "str你输入的除数为0";
}
cout << "继续执行" << endl;
return x*1.0 / y;
}
/*catch (const char * errorstr)
{
if (code == 1)
{
cout << "错误" << endl;
}
else if (code == 2)
{
cout << "除数为0" << endl;
}
return -1.0;*/
catch (const char *errorstr)
{
if (strcmp(errorstr, "str你输入的被除数为0")==0)
{
cout << errorstr << endl;
}
else if (strcmp(errorstr, "str你输入的被除数为0") == 0)
{
cout << errorstr << endl;
}
return 0;
}
}
void main()
{
int x, y;
cin >> x >> y;//输入xy
cout << "x=" << x << endl
<< "y=" << y << endl
//<<"x/y="<<divv(x,y)<<endl;
<< "x/y=" << divvstr(x, y) << endl;
system("pause");
}
x/y因为没有返回值,系统选的垃圾值