zoukankan      html  css  js  c++  java
  • 异常(3)


    #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;
    }

  • 相关阅读:
    每日总结32
    每日总结31
    每日总结30
    Leetcode 115. 不同的子序列(二维DP)
    Leetcode 59. 螺旋矩阵 II
    Leetcode 227. 基本计算器 II
    macOS下将GitHub中单个子文件夹下载到本地
    P3796 【模板】AC自动机(加强版)
    P3808 【模板】AC自动机(简单版)
    【Kubernetes】副本的删除
  • 原文地址:https://www.cnblogs.com/w413133157/p/1666779.html
Copyright © 2011-2022 走看看