| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 592 人关注过本帖
标题:异常处理失败
只看楼主 加入收藏
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
 问题点数:0 回复次数:1 
异常处理失败
下面这个程序,编译是通过的,但是运行阶段出现了错误,请大家指点一下.谢谢!
//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);
}
搜索更多相关主题的帖子: 失败 
2008-09-30 11:00
快速回复:异常处理失败
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020225 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved