zoukankan      html  css  js  c++  java
  • 异常类的构建——5个子类构建

    1.Exception.h 中增加ArithmetricException类

    class ArithmetricException:public Exception
    {
    public:
        ArithmetricException():Exception(0) {}   //我认为这个实现没有必要
        ArithmetricException(const char* message):Exception(message) {}
        ArithmetricException(const char* file, int line):Exception(file, line) {}
        ArithmetricException(const char* message, const char* file, int line):Exception(message,file,line) {}
    
        ArithmetricException(const ArithmetricException& e):Exception(e) {}
        ArithmetricException& operator = (const ArithmetricException& e)
        {
            Exception::operator =(e);
    
            return *this;
        }
    
    };

    2.Exception.h 中增加NullPointerException

    class NullPointerException:public Exception
    {
    public:
        NullPointerException():Exception(0) {}   //我认为这个实现没有必要
        NullPointerException(const char* message):Exception(message) {}
        NullPointerException(const char* file, int line):Exception(file, line) {}
        NullPointerException(const char* message, const char* file, int line):Exception(message,file,line) {}
    
        NullPointerException(const NullPointerException& e):Exception(e) {}
        NullPointerException& operator = (const NullPointerException& e)
        {
            Exception::operator =(e);
    
            return *this;
        }
    
    };

    3.Exception.h 中增加IndexOutOfBoundsException

    class IndexOutOfBoundsException:public Exception
    {
    public:
        IndexOutOfBoundsException():Exception(0) {}   //我认为这个实现没有必要
        IndexOutOfBoundsException(const char* message):Exception(message) {}
        IndexOutOfBoundsException(const char* file, int line):Exception(file, line) {}
        IndexOutOfBoundsException(const char* message, const char* file, int line):Exception(message,file,line) {}
    
        IndexOutOfBoundsException(const IndexOutOfBoundsException& e):Exception(e) {}
        IndexOutOfBoundsException& operator = (const IndexOutOfBoundsException& e)
        {
            Exception::operator =(e);
    
            return *this;
        }
    
    };

    4.Exception.h 中增加NoEnoughMemoryException

    class NoEnoughMemoryException:public Exception
    {
    public:
        NoEnoughMemoryException():Exception(0) {}   //我认为这个实现没有必要
        NoEnoughMemoryException(const char* message):Exception(message) {}
        NoEnoughMemoryException(const char* file, int line):Exception(file, line) {}
        NoEnoughMemoryException(const char* message, const char* file, int line):Exception(message,file,line) {}
    
        NoEnoughMemoryException(const NoEnoughMemoryException& e):Exception(e) {}
        NoEnoughMemoryException& operator = (const NoEnoughMemoryException& e)
        {
            Exception::operator =(e);
    
            return *this;
        }
    
    };

    5.Exception.h 中增加InvalidParameterException

    class InvalidParameterException:public Exception
    {
    public:
        InvalidParameterException():Exception(0) {}   //我认为这个实现没有必要
        InvalidParameterException(const char* message):Exception(message) {}
        InvalidParameterException(const char* file, int line):Exception(file, line) {}
        InvalidParameterException(const char* message, const char* file, int line):Exception(message,file,line) {}
    
        InvalidParameterException(const InvalidParameterException& e):Exception(e) {}
        InvalidParameterException& operator = (const InvalidParameterException& e)
        {
            Exception::operator =(e);
    
            return *this;
        }
    
    };

    main.cpp

    #include <iostream>
    #include "Exception.h"
    
    using namespace std;
    using namespace DTLib;
    
    
    int main()
    {
        try
        {
            THROW_EXCEPTION(ArithmetricException,"test");
        }
    
        catch(const ArithmetricException& e)
        {
            cout << " catch(const ArithmetricException& e)" << endl;
            cout << e.message() << endl;
            cout << e.location() << endl;
        }
    
        catch(const Exception& e)
        {
            cout << " catch(const Exception& e)" << endl;
            cout << e.message() << endl;
            cout << e.location() << endl;
        }
        return 0;
    }

    设计原则:
    在可复用代码库设计时,尽量使用面向对象技术进行架构,尽量使用异常处理机制分离正常逻辑和异常逻辑

  • 相关阅读:
    libcurl进行HTTP GET获取JSON数据(转载)
    c/c++ 动态申请数组(转载)
    利用注册表写开机启动程序(转载)
    VC++使用IMAPI调用Outlook邮箱客户端和Foxmail邮箱客户端遇到的问题
    解决Duilib集成CEF浏览器在Win10无法向客户区拖拽文件
    解决往监控目录拖拽文件夹无法监控到的问题
    VC++ 实现修改文件创建、访问、修改时间属性(转载)
    Windows中的时间(SYSTEMTIME和FILETIME) (转载)
    ThinkPHP真正疑难问题笔记
    git版本控制管理实践-4
  • 原文地址:https://www.cnblogs.com/-glb/p/12037867.html
Copyright © 2011-2022 走看看