当我的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) 我正在使用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.我的问题是:如果我不想使用静态演员,我应该抓住什么类型?