相关疑难解决方法(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万
查看次数

如果我抛出一个字符串文字,我应该抓住什么类型?

我正在使用Linux下的g ++在C++中编写一个非常简单的应用程序,我试图将一些原始字符串作为异常(是的,我知道,这不是一个好的做法).

我有以下代码(简化):

int main()
{
  try
  {
    throw "not implemented";

  }
  catch(std::string &error)
  {
    cerr<<"Error: "<<error<<endl;
  }
  catch(char* error)
  {
    cerr<<"Error: "<<error<<endl;
  }
  catch(...)
  {
    cerr<<"Unknown error"<<endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

Unknow error上了控制台.但是,如果我将文字字符串静态转换为其中任何一个,std::string或者按预期char *打印Error: not implemented.我的问题是:如果我不想使用静态演员,我应该抓住什么类型?

c++ linux g++ exception

22
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×2

exception ×2

g++ ×1

linux ×1

stl ×1