网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

一 C++异常的简介

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

创新互联公司一直通过网站建设和网站营销帮助企业获得更多客户资源。 以"深度挖掘,量身打造,注重实效"的一站式服务,以网站设计制作、网站设计、移动互联产品、成都全网营销推广服务为核心业务。十多年网站制作的经验,使用新网站建设技术,全新开发出的标准网站,不但价格便宜而且实用、灵活,特别适合中小公司网站制作。网站管理系统简单易用,维护方便,您可以完全操作网站资料,是中小公司快速网站建设的选择。

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

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

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

    if(!((-delta

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

//代码示例
#include 
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类为纯虚父类,所有子类都能进行动态类型识别
        */

当前文章:数据结构--异常类与顶层父类的实现
网页链接:http://bjjierui.cn/article/pjhhcg.html