异常类的构建——

£可爱£侵袭症+ 提交于 2019-12-14 00:14:37

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

};

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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!