异常处理失败
下面这个程序,编译是通过的,但是运行阶段出现了错误,请大家指点一下.谢谢!//exc_mean.h --包含了异常类#include <iostream>
class BadMean
{
private:
double v1;
double v2;
public:
BadMean(double a=0, double b=0) : v1(a), v2(b){}
void Mesg();
};
inline void BadMean::Mesg()
{
std::cout << "hmean(" << v1 << "," << v2 << "):"
<< "invalid arguments: a=-b\n";
}
class BadGmean
{
public:
double v1;
double v2;
BadGmean(double a=0, double b=0) : v1(a),v2(b){}
const char *Mesg();
};
inline const char *Mesg()
{
return "gmean() arguments should be >=0\n";
}
//error4.cpp--main() 函数#include <iostream>
#include <cmath>
#include "exc_mean.h"
using namespace std;
//function prototypes
double hmean(double a, double b) throw(BadMean);
double gmean(double a, double b) throw(BadGmean);
int main()
{
double x, y, z;
cout << "Enter two numbers: ";
while(cin >> x >> y)
{
try{
z=hmean(x, y);
cout << "Harmonic mean of " << x << " and " << y << " is " << z <<endl;
cout << "Geometric mean of " << x << " and " << y << " is "
<< gmean(x, y) <<endl;
cout << "Enter next set numbers <q to quit>:";
}
catch(BadMean &bg)
{
bg.Mesg();
cout << "try again!\n";
continue;
}
catch(BadGmean &hg)
{
cout << hg.Mesg();
cout << "Value used: " << hg.v1 << ", " << hg.v2 << endl;
cout << "Sorry,you don't get to play any more.\n";
break;
}// end of catch block
}
cout << "Bye!\n";
return 0;
}
double hmean(double a, double b) throw(BadMean)
{
if(a == -b)
throw BadMean(a,b);
return 2.0*a*b/(a+b);
}
double gmean(double a,double b) throw(BadGmean)
{
if(a<0 || b<0)
throw BadGmean(a, b);
return sqrt(a*b);
}