组织和编写异常的正确方法 类

Right way to organize and write exception classes

我的任务是:基于我的项目,创建异常 classes,从 std::exception 交付并实现虚函数 what()。

我可能在我的项目中得到的异常可能只是在 reading/writing 文件、converting/reading 数据类型、创建不同的 class 成员等过程中引起的。 我试过这样走:

#include <stdexcept>
#define MYEXCEPTION(exception_name) 
struct exception_name : public ::std::runtime_error  
{                                                   
typedef ::std::runtime_error                    
Base;                                      
explicit exception_name(const std::string& msg) 
    : Base(msg) {}                               
    explicit exception_name(const char* msg)        
    : Base(msg) {}                               
};

#include <iostream>
#include <string>
#include <exception>
class Catch: public std::exception
{
public:
    Catch() :exceptionCode(0), exceptionSource("no source"), exceptionMessage("no message") {SetError(my_ex);}
    explicit Catch(std::string message) :exceptionCode(0), exceptionSource("no source"), exceptionMessage(std::move(message)) { SetError(my_ex); }
    Catch(std::string source, std::string message) :exceptionCode(0), exceptionSource(std::move(source)), exceptionMessage(std::move(message)) { SetError(my_ex); }
    Catch(int code, std::string source, std::string message):exceptionCode(code), exceptionSource(std::move(source)), exceptionMessage(std::move(message)) { SetError(my_ex); }
    const char* what() const noexcept   {       return ((*error).c_str());  }

private:
    const std::string my_ex = ("CODE:" + std::to_string(exceptionCode) + "\n" + "SOURCE:" + exceptionSource + "\n" + "MESSAGE:" + exceptionMessage + "\n");
    void SetError(std::string ex) { error = &ex; }

    int exceptionCode;
    std::string exceptionSource;
    std::string exceptionMessage;

    std::string* error;
};

class ObjectException : public Catch {
public:
    ObjectException(char* message, int state): Catch(message) {     this->state = state;    }
    int GetState(void) { return state; }
private:
    int state;
};

class FileException : public std::exception {
    FileException(std::string destination, std::string path, std::string message): message(message.c_str()) {}
    const char* what() const throw() { return  "FileException occured\n"; }
private:
    std::string message;
    std::string destination;
};

class DataException : public std::exception {
public:
    DataException(char* message, int state) :exception(message) {
        this->state = state;
    }
    int GetState(void) { return state; }
    const char* what() const throw() { return  "DataException:invalid_type\n"; };
private:
    int state;  
};

class Exception : public std::exception
{
    std::string _msg;
public:
    Exception(const std::string& msg) : _msg(msg) {}

    virtual const char* what() const noexcept override
    {
        return _msg.c_str();
    }
};

但是,当我尝试时:

#include "catch.h"
void Polygon_m::Define() {

    std::ifstream input;
    input.open(path);
    if (input.is_open())
        std::cout << "Polygon_m::Defining_file:found" << std::endl;
    else
        throw  FileException("Polygon_m::Defining_file:", path , ":failed");
    ..........
}

我有: FileException::FileException(...)(在行中声明...)不可访问

请给我一些技巧,我整个星期都在努力探索这个话题。它必须非常简单。

我已经看了很多与我的问题相关的帖子,但问题是我对这个话题没有感觉。我找不到任何一本书或一篇文章来解释我需要的细节。如果你知道,请给我一个link。

FileException::FileException无法访问的原因是它在class块中的任何访问说明符之前声明,并且class默认为private访问。

在 class 的顶部添加 public

此外,Catch 的成员之间存在依赖性问题。 my_ex 被定义为根据几个后续成员的表达式。但是由于成员是按声明顺序初始化的,它总是会读取 exceptionCode 等未初始化的值。尝试将 my_ex 改为函数。