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

标签 c++ exception

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

我在项目中可能遇到的异常可能只是在读取/写入文件、转换/读取数据类型、创建不同的类成员等过程中引起的。 我试着走这条路:

#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(...)(在行中声明...)不可访问

请给我一些如何去做的提示,我整个星期都在努力发现这个话题。它一定非常简单。

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

最佳答案

FileException::FileException 无法访问的原因是它在class block 中的任何访问说明符之前声明,并且class 默认到私有(private)访问权限。

在类的顶部添加public

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

关于c++ - 组织和编写异常类的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161347/

相关文章:

加载共享库时出现 C++ 错误 : libopencv_ximgproc. so.4.4

c++ - 优化循环并避免模板特化中的代码重复

c++ - Visual Studio 2008 中的非托管 C++ 单元测试

python - 即使通过代理的连接失败,如何重试当前循环

Python 2.6 文件存在 errno 17,

c++ - gcount、tellg 和 seekg 中的 g 代表什么?

c++ - 列表初始化(使用花括号)有什么好处?

c# - 两个异常管理问题

Java 声音 |不支持的音频文件异常

java - 无法找到我的 Java 纸牌游戏的 hasNext() 和 next() 方法的符号