异常类的构建

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

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

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

时间: 2024-07-29 11:13:54

异常类的构建的相关文章

第十一课、异常类的构建-------------狄泰软件学院

一.自定义异常类 1.异常的类型可以是自定义的类类型 2.对于类类型的匹配依旧是之上而下的严格匹配 3.赋值兼容性原则在异常匹配中依然适用 所以要 (1).匹配子类异常的catch放在上部 (2).匹配父类异常的catch放在下部 4.异常类是数据结构所依赖的"基础设施"(现代c++库也必然包含充要的异常类族) 二.一步步打造自己的异常类 1.首先是抽象类EXception的编写,既然是抽象类,必然含有纯虚函数,通常的做法都是将析构函数作为纯虚函数 头文件:接口定义 class Exc

异常类的构建(四)

我们在之前学习了 C++ 中有关异常的知识,现在我们来重新回顾下.那么异常的格式是什么呢?便是 try ... catch ...:try 语句处理正常的代码逻辑,而 catch 语句则处理异常情况,try 语句中的异常由对应的 catch 语句处理.格式如下 try {     double r = divide(1, 0); } catch(...) {     cout << "Divided by zero ..." << endl; } 在 C++ 中

Cpp数据结构实战开发2-基本类的构建

构建自己的类库,MxLib 迭代开发 单一继承树:所有类继承自Object类,规范堆对象创建时的行为 只抛异常,不处理:使用宏抛出异常,提高可移植性 弱耦合性:尽量不使用标准库中的类和函数,提高可移植性 顶层父类 软件架构实践经验: 尽量使用单重继承的方式进行系统设计 尽量保持系统中只存在单一的继承树 尽量使用组合关系代替继承关系 在MxLib中创建Objec类,所有类都继承自MxLib::Object类,统一定义动态内存申请和销毁的行为 头文件 Object.h #ifndef OBJECT_

Asp.net Core WebApi 全局异常类

通过全局异常类,所有程序中遇到的错误都会被拦截,并友好的返回结果. 1.自定义一个全局异常处理类中间件 using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; using Syst

java中异常类与类别

异常类的层次结构: 在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出).Throwable 指定代码中可用异常传播机制通过 Java 应用程序传输的任何问题的共性,它们都在java.lang包下面. Error类标记了严重错误,类似内存溢出,虚拟机异常,等等,是不该出现的.这些错误表示故障发生于虚拟机自身.或者发生在虚拟机试图执行应用时,如Java虚拟机运行错误(Virtual MachineError).类定义错误(NoClassDefFoundError)等.这些错

借助backtrace和demangle实现异常类Exception

C++的异常类是没有栈痕迹的,如果需要获取栈痕迹,需要使用以下函数: #include <execinfo.h> int backtrace(void **buffer, int size); char **backtrace_symbols(void *const *buffer, int size); void backtrace_symbols_fd(void *const *buffer, int size, int fd); backtrace将当前程序的调用信息存储在buffer中

类的继承与super()的意义以即如何写一个正确的异常类

这些东西都是我看了许多名师课程和自己研究的成果,严禁转载,这里指出了如何正确的自己定义一个异常类并看一看sun写的java的源代码话题一:子类的构造器执行是否一定会伴随着父类的构造执行? 1.this()与super()不能共存2.如果不显示地调用this()和super();子类对象的创建是否一定执行父类的构造3.既然super()和this()不能共存,又说子类的构造执行一定会执行父类的构造,那么我让子类的构造执行this()是不是就不能在执行父类的构造? 4.如果我非不让父类的构造执行,我

java中常见的异常类

1. java.lang.nullpointerexception   这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等.对数组操作中出现空指针,很多情况下是一些刚开始学习编程的朋友常犯的错误,即把数组的初始化和数组元素的初始化混淆起来了.数组的初始化是对数组分配需要的空间,而初始化后的数组,其中的元素并没有实例化,依然是空

C++异常类的定义和使用以及我对异常类的理解

异常类的作用就是在执行我们自己的某个功能函数出现意外情况时,为了方便查找错误出处,可以在意外发生时抛出异常(1).首先定义自己的异常类 可以直接定义也可以从标准异常类派生    class  CEGUIEXPORT Exception    {    public:        virtual ~Exception(void);        const String& getMessage(void) const  {return d_message;}        const Strin