C++异常类

c++异常类

  • Exception,接口类
  • ArithmeticException类, 计算异常
  • NullPointerException类,空指针异常
  • IndexOutOfBoundsException类,越界异常
  • NoEnoughtMemoryException,内存不足异常
  • InvalidParameterException,参数错误异常
//Exception.h
#define THROW_EXCEPTION(e,m)  (throw  e(m,__FILE__,__LINE__))

class Exception
{
protected:
    char* m_message;
    char* m_location;
    void init(const char* messge, const char* file, int line);
public:
    Exception(const char* message);
    Exception(const char* file, int line);
    Exception(const char* messge, const char* file, int line);

    Exception(const Exception& e);
    Exception& operator= (const Exception& e);

    virtual const char* message()const;
    virtual const char* location()const;

    virtual ~Exception()=0;
};

class ArithmeticException : public Exception
{
public:
    ArithmeticException ():Exception(0) { }
    ArithmeticException (const char* message):Exception(message) { }
    ArithmeticException (const char* file,int line):Exception(file,line) { }
    ArithmeticException (const char* message,const char* file,int line) :Exception(message,file,line) { }

    ArithmeticException& operator =(const ArithmeticException& e)
    {
        Exception::operator =(e);
        return *this;
    }
};

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& operator =(const  NullPointerException& e)
    {
        Exception::operator =(e);
        return *this;
    }
};

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& operator =(const  IndexOutOfBoundsException& e)
    {
        Exception::operator =(e);
        return *this;
    }
};

class  NoEnoughtMemoryException : public Exception
{
public :
    NoEnoughtMemoryException ():Exception (0) {}
    NoEnoughtMemoryException (const char* message):Exception(message) {}
    NoEnoughtMemoryException(const char * file,int line):Exception(file,line){}
    NoEnoughtMemoryException(const char* message,const char *file, int line):Exception(message,file,line){}
    NoEnoughtMemoryException& operator =(const  NoEnoughtMemoryException& e)
    {
        Exception::operator =(e);
        return *this;
    }
};
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& operator =(const  InvalidParameterException& e)
    {
        Exception::operator =(e);
        return *this;
    }
};

class  InvalidOperationException : public Exception
{
public :
    InvalidOperationException ():Exception (0) {}
    InvalidOperationException (const char* message):Exception(message) {}
    InvalidOperationException(const char * file,int line):Exception(file,line){}
    InvalidOperationException(const char* message,const char *file, int line):Exception(message,file,line){}
    InvalidOperationException& operator =(const  InvalidParameterException& e)
    {
        Exception::operator =(e);
        return *this;
    }
};
//Exception.cpp
 void Exception::init(const char* messge, const char* file, int line)
    {
        m_message = (messge ? strdup(messge) : NULL);
        if (file != NULL)
        {
            char sl[16] = { 0 };

            snprintf(sl, sizeof(sl), "%d", line);
            m_location = static_cast<char*>(malloc(strlen(file) + strlen(sl) + 2));

            if (m_location != NULL)
            {
                strcpy(m_location,file);
                strcat(m_location, ":");
                strcat(m_location,  sl);
            }
        }
        else
        {
            m_location = NULL;
        }
    }

    Exception::Exception(const char* message)
    {
        init(message, NULL, 0);
    }
    Exception::Exception(const char* file, int line)
    {
        init(NULL, file, line);
    }
    Exception::Exception(const char* messge, const char* file, int line)
    {
        init(messge, file, line);
    }

    Exception::Exception(const Exception& e)
    {
        m_message = strdup(e.m_message);
        m_location = strdup(e.m_location);
    }
    Exception& Exception::operator= (const Exception& e)
    {
        if (this != &e)
        {
            free(m_message);
            free(m_location);
            m_message = strdup(e.m_message);
            m_location = strdup(e.m_location);
        }
        return *this;
    }

    const char* Exception::message()const
    {
        return m_message;
    }
    const char* Exception::location()const
    {
        return m_location;
    }

    Exception::~Exception()
    {
        free(m_message);
        free(m_location);
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值