C++ Cpp::自定义异常,未捕获变量消息(

C++ Cpp::自定义异常,未捕获变量消息(,c++,exception,error-handling,macros,try-catch,C++,Exception,Error Handling,Macros,Try Catch,您好,我的异常类/宏当前有问题 所以首先是一个示例文件来说明我的意思 #include <iostream> #include <string> class ExceptionHelper : public std::exception { public: ExceptionHelper(std::string msg) : std::exception(), msg_(msg) {} ExceptionHelper operator<<(std:

您好,我的异常类/宏当前有问题

所以首先是一个示例文件来说明我的意思

#include <iostream>
#include <string>

class ExceptionHelper : public std::exception {
public:
  ExceptionHelper(std::string msg) : std::exception(), msg_(msg) {}

  ExceptionHelper operator<<(std::string const &value) {
    msg_.append(" :: ");
    msg_.append(value);
    return ExceptionHelper(msg_);
  }

  virtual const char *what() const throw() { return msg_.c_str(); }

private:
  std::string msg_;
};

#define DEFINE_EXCEPTION(ClassName, Message)                                   \
  class ClassName : public ExceptionHelper {                                   \
  public:                                                                      \
    ClassName(std::string msg = Message) : ExceptionHelper(msg) {}             \
  };

DEFINE_EXCEPTION(Test, "Testmessage")

int main() {
  try {
    throw Test();
  } catch (Test &e) {
    std::cout << "I was caught 1 " << e.what() << std::endl;
  }

  try {
    throw(Test() << "Something added");
  } catch (Test &e) {
    std::cout << "I was caught 2 " << e.what();
  } catch (std::exception &e) {
    std::cout << "should not be called " << e.what() << std::endl;
  }

  try {
    Test t;
    t << "Something added";
    throw t;
  } catch (Test &e) {
    std::cout << "I was caught 3 " << e.what();
  } catch (std::exception &e) {
    std::cout << "should not be called2" << e.what() << std::endl;
  }
}
想要的输出:

I was caught 1 Testmessage
I was caught 2 Testmessage :: Something added
I was caught 3 Testmessage :: Something added
因此,我想用DEFINE_Exception宏创建特殊的异常类,该宏非常有效


我现在想向一个新的异常类型添加额外的信息。这对于调试来说很方便,因为它允许我编写类似于抛出测试的东西ExceptionHelper::operator谢谢您的提示:。。。漫长的一天

如果有人对工作版本感兴趣,请点击此处

class ExceptionHelper : public std::exception {
public:
  ExceptionHelper(std::string msg) : std::exception(), msg_(msg) {}
  virtual const char *what() const throw() { return msg_.c_str(); }

protected:
  std::string msg_;
};

#define DEFINE_EXCEPTION(ClassName, Message)                                   \
  class ClassName : public ExceptionHelper {                                   \
  public:                                                                      \
    ClassName(std::string msg = Message) : ExceptionHelper(msg) {}             \
                                                                               \
    template<class T>                                                          \
    ClassName &operator<<(T const &value) {                                    \
      msg_.append(" :: ");                                                     \
      msg_.append(value);                                                      \
      return *this;                                                            \
    }                                                                          \
  };

这是我的第一个版本,我也这么做了。我改变了它,因为在第三个例子中,它的工作原理。所以我想问题可能是函数返回了一个引用。。。但是它没有什么区别。它最好返回一个引用,这样它在返回时就不会分割派生类了吗?不。你必须返回你想要捕获的类型。或者进入虚拟函数并捕捉ExceptionHelper指针。他想要测试这可能是一个模板吗?是的,我实际上是作为一个模板版本来做的,并且它的工作方式是相同的。这里的许多人会因为你这样做而拥抱你。你要把它添加到你的答案中吗?很好。我很想这样做,但是stackoverflow迫使我等了两天。我不知道为什么。我会在我有能力的时候做这件事
I was caught 1 Testmessage
I was caught 2 Testmessage :: Something added
I was caught 3 Testmessage :: Something added
class ExceptionHelper : public std::exception {
public:
  ExceptionHelper(std::string msg) : std::exception(), msg_(msg) {}
  virtual const char *what() const throw() { return msg_.c_str(); }

protected:
  std::string msg_;
};

#define DEFINE_EXCEPTION(ClassName, Message)                                   \
  class ClassName : public ExceptionHelper {                                   \
  public:                                                                      \
    ClassName(std::string msg = Message) : ExceptionHelper(msg) {}             \
                                                                               \
    template<class T>                                                          \
    ClassName &operator<<(T const &value) {                                    \
      msg_.append(" :: ");                                                     \
      msg_.append(value);                                                      \
      return *this;                                                            \
    }                                                                          \
  };