数据结构--异常类与顶层父类的实现

一 C++异常的简介

C++内置了异常处理的语法元素try.....catch.....
1.try语句处理正常代码逻辑
2.catch语句处理异常情况
3.try语句中的异常由对应的catch语句处理
常见的语句如下

try
{
    double r=divide(1,0);
}
catch(...)
{
    cout<<"the result..."<<endl;
}

C++通过throw语句抛出异常信息

double divide(double a,double b)
{
    const double delta=0.00000000001;
    double ret=0;

    if(!((-delta<b)&&(b<delta)))
    {
        ret=a/b;
    }
    else
    {
        throw 0;//产生除0异常
     }
    return ret;
}

C++异常处理分析
throw抛出的异常必须被catch处理--当前函数能够处理异常,程序机械往下执行,当前函数无法处理异常,则函数停止执行,并返回.未被处理的异常会顺着函数调用栈向上传播,直到被处理为止,否则程序将停止执行
数据结构--异常类与顶层父类的实现

//代码示例
#include <iostream>
using namespace std;
double divide(double a, double b)
{
    const double delta = 0.000000000000001;
    double ret = 0;

    if( !((-delta < b) && (b < delta)) ) {
        ret = a / b;
    }
    else {
        throw 0;   // 产生除 0 异常
    }

    return ret;
}

void Demo1()
{
    try
    {
        throw 'c';
    }
    catch(int i)
    {
        cout << "catch(int i)" << endl;
    }
    catch(double d)
    {
        cout << "catch(double d)" << endl;
    }
    catch(char c)
    {
        cout << "catch(char c)" << endl;
    }
}

void Demo2()
{
    throw 0.0001; // "D.T.Software";   // const char*
}

int main()
{
    cout << "main() begin" << endl;

    try
    {
        double c = divide(1, 1);

        cout << "c = " << c << endl;
    }
    catch(...)
    {
        cout << "Divided by zero..."  << endl;
    }

    Demo1();

    try
    {
        Demo2();
    }
    catch(char* c)
    {
        cout << "catch(char* c)" << endl;
    }
    catch(const char* cc)
    {
        cout << "catch(char* cc)" << endl;
    }
    catch(...)
    {
        cout << "catch(...)" << endl;
    }

    cout << "main() end" << endl;

    return 0;
}

二 异常类的构建

异常的类型可以是自定义类类型,对于类类型异常的匹配依旧是至上而下严格匹配,赋值兼容性原则在异常匹配中依然适用
异常类的功能定义

异常类功能定义
ArithmeticException计量异常
NullPointerException空指针异常
IndexOutBoundsException越界异常
NoEnoughMemoryException内存不足异常
InvaildParameterException参数异常

数据结构--异常类与顶层父类的实现

#ifndef EXCEPTION_H
#define EXCEPTION_H
/*
   异常的类型可以是自定义类类型
   对于类类型异常的匹配依旧是至上而下严格匹配

   一般而言
      匹配子类异常的catch放在上部
      匹配父类异常的catch放在下部
 */
#include "Object.h"

namespace MyLib
{
//定义宏THROW_EXCEPTION,抛出异常时直接写异常类型及异常信息即可
#define THROW_EXCEPTION(e, m) (throw e(m, __FILE__, __LINE__))

    class Exception:public Object
    {
    protected:
        char* m_message;//异常的信息
        char* m_location;//异常的位置

        void init(const char* message,const char *file,int line);
    public:
        Exception(const char* message);
        Exception(const char *file,int line);
        Exception(const char* message,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();//纯虚函数
    };

    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(const ArithmeticException& e):Exception(e){}//拷贝构造函数

        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(const NullPointerException& e):Exception(e){}//拷贝构造函数

        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(const indexOutOfBoundsException& e):Exception(e){}//拷贝构造函数

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

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

        NoEoughMemoryException(const NoEoughMemoryException& e):Exception(e){}//拷贝构造函数

        NoEoughMemoryException&operator =(const NoEoughMemoryException& 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(const InvalidOperationException& e):Exception(e){}//拷贝构造函数

        InvalidOperationException&operator =(const InvalidOperationException& 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(const InvalidParameterException& e):Exception(e){}//拷贝构造函数

        InvalidParameterException&operator =(const InvalidParameterException& e)
        {
            Exception::operator =(e);
            return *this;
        }
    };
}
#endif // EXCEPTION_H

三 顶层父类的创建
创建顶层父类的意义--遵循经典设计准则,所有的数据都继承Object类,定义动态内存申请的行为,提高代码的移植性
顶层父类的接口定义

    class Object
    {
          public:
            void* operator new(unsigned int size)throw();
            void operator delete(void* p);
            void* operator new[](unsigned int size)throw();
            void operator delete[](void* p);

            ~Object();
    };
        /*
        Object类是所写的顶层父类
        Object类用于统一内存申请的行为
        在堆中创建Object子类的对象,失败时返回NULL值
        Object类为纯虚父类,所有子类都能进行动态类型识别
        */

转载于:https://blog.51cto.com/13475106/2340447

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值