#include "stdafx.h"
#include <iostream.h>
#include <string.h>

//
类描述异常信息
class  Matherr
{
protected:
    char m_szMsg[32];
public:
    virtual const char *GetMsg()const
    {
        return m_szMsg;
    }
};

class  Overflow:public Matherr
{
public:
    Overflow()
    {
        strcpy(m_szMsg, "Overflow");
    }
    const char *GetMsg()const
    {
        cout << "Overflow::GetMsg()" << endl;
        return m_szMsg;
    }
};

class  Underflow:public Matherr
{
public:
    Underflow()
    {
        strcpy(m_szMsg, "Underflow");
    }
    const char *GetMsg()const
    {
        cout << "Underflow::GetMsg()" << endl;
        return m_szMsg;
    }
};

class  Zerodivide:public Matherr
{
public:
    Zerodivide()
    {
        strcpy(m_szMsg,"Zerodivide");
    }
    const char *GetMsg() const
    {
        cout << "Zerodivide::GetMsg()" << endl;
        return m_szMsg;
    }
};

int main(int argc, char* argv[])
{   
    try
    {
        //
下面两个子类的异常都会初下面的基类接收
        throw Zerodivide();
        throw Overflow();
    }
    //
基类来接收异常
    catch (Matherr &theMat)
    {
        cout << theMat.GetMsg() << endl;
    }
    return 0;
}

posted on 2010-02-09 19:55  o无尘o  阅读(185)  评论(0编辑  收藏  举报