温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-14 18:20:14 来源:网络 阅读:442 作者:淡淡_小孩 栏目:编程语言

一 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类为纯虚父类,所有子类都能进行动态类型识别
        */
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI