相关疑难解决方法(0)

c ++异常:抛出std :: string

当我的C++方法遇到奇怪但无法恢复的东西时,我想抛出异常.投掷是否可以std::string指针可以吗?

这是我期待的事情:

void Foo::Bar() {
    if(!QueryPerformanceTimer(&m_baz)) {
        throw new std::string("it's the end of the world!");
    }
}

void Foo::Caller() {
    try {
        this->Bar(); // should throw
    }
    catch(std::string *caught) { // not quite sure the syntax is OK here...
        std::cout << "Got " << caught << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ stl exception

74
推荐指数
6
解决办法
15万
查看次数

自定义异常类的C++语法

我是C++的新手,并且发现了以下代码片段,用于从std :: exception扩展的自定义异常.我不理解的唯一部分是: err_msg(msg) {}构造函数定义之后.任何人都可以解释为什么这不是在函数括号中?

class my_exception : public std::exception {
  private:
    std::string err_msg;

  public:
    my_exception(const char *msg) : err_msg(msg) {};
    ~my_exception() throw() {};
    const char *what() const throw() { return this->err_msg.c_str(); };
};
Run Code Online (Sandbox Code Playgroud)

c++ exception custom-exceptions

5
推荐指数
1
解决办法
9061
查看次数

标签 统计

c++ ×2

exception ×2

custom-exceptions ×1

stl ×1