#include<iostream>
#include<cmath>
using namespace std;
struct Res {
float x1, x2;
};
Res resolution(int a, int b, int c)throw(int);
int main()
{
int a, b, c;
Res r;
cout << "Input a,b,c";
cin >> a >> b >> c;
try
{
r = resolution(a, b, c);
cout << "x1=" << r.x1 << "\tx2=" << r.x2 << endl;
}
catch (int)
{
cerr << "Sqrt Negative Exception" << endl;
}
//catch (...)
//{
// cerr << "unexpected or rethrow exception!" << endl;
//}
return 0;
}
float quotient(int a, int b)throw(char*)
{
if (b == 0)
{
throw("Divide 0!");
}
else
return a / (float)b;
}
Res resolution(int a, int b, int c)
{
Res tmpr;
try
{
if (a == 0 && b != 0)
{
tmpr.x1 = tmpr.x2 = quotient(-c, b);
return tmpr;
}
if (b * b - 4 * a * c < 0)
throw(b);
tmpr.x1 = quotient(-b + sqrt(b * b - 4.0 * a * c), 2 * a);
tmpr.x2 = quotient(-b - sqrt(b * b - 4.0 * a * c), 2 * a);
return tmpr;
}
catch (char* Errs)
{
cerr << Errs << endl;
//exit(0);
//throw;
}
}
当我输入 0 0 3
的时候 , 不能正常调出来"Divide 0!这句话 为什么
为什么报错,跟正确代码一样,我是vs2022
“Divide 0!”这是字面量,就是const char* ,你catch(char*) 所以不能捕获,改成 catch(const char*)