码迷,mamicode.com
首页 > 其他好文 > 详细

异常类的构建

时间:2019-12-14 09:18:15      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:throw   设计原则   http   microsoft   fbo   end   exce   amp   inf   

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

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

技术图片

 

异常类的构建

标签:throw   设计原则   http   microsoft   fbo   end   exce   amp   inf   

原文地址:https://www.cnblogs.com/-glb/p/12037867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!