如何记录c ++异常

时间:2010-01-22 11:20:42

标签: c++ exception-handling

您知道如何记录异常吗? 现在,catch语句中的消息被打印出来了,但是我无法理解为什么管理器.Gere()被称为susely。

try{
    Manager.Gere(&par,&Acc, coman, comando, RunComando, log, &parti, comandosS, RunComandosSuper,true);
}
catch (...)
{
    log("ERROR ENTER GERE*****");
}


Perif::Gere(CString *par, CString *Acc, HANDLE coman, HANDLE comando, HANDLE RunComando, Log &log, CString *parti, HANDLE comandosS, HANDLE RunComandosSuper,bool first)
{
    log->LogD("Perif :: Gere Enter****** "); //It doesnt get printed

}

4 个答案:

答案 0 :(得分:2)

行为良好的API应该只抛出从std::exception派生的类型的对象。如果是这种情况,那么异常将包含一个包含消息的成员函数const char *what(),这将有希望描述错误。所以你可以试试这个,并希望它有所帮助:

try {
    Manage.Gere(...);
} catch (const std::exception &e) {
    log(e.what());
} catch (...) {
    log("Manage.Gere threw unknown exception");
}

如果它抛出的类型不是std::exception,那么您需要查看该函数的文档和/或源代码,以查看可能出错的类型以及它抛出的类型。如果没有这个,我会寻找一个更好的图书馆。

答案 1 :(得分:0)

首先你需要找到Manage.Gere可以抛出的异常 然后catch他们特别喜欢catch(FirstExceptionGereThrows &exc),当你发现所有可能的例外时,你就会知道Manage.Gere中的失败。

catch(FirstException &exc){
   log << "Failed because FirstException\n";
}catch(SecondException &exc){
   log << "Failed because SecondException\n";
}

之后,如果你很幸运,Manage.Gere抛出的异常可能包含一些关于崩溃的额外信息,你也可以记录。

catch(FirstException &exc){
   log << "Failed because FirstException: " << exc.what() << "\n";
}

答案 2 :(得分:0)

正如其他人所说,RTFM就是答案。但是,我发现懒惰的程序员经常编写如下代码:

if ( something ) {
   throw "Something has happened!";
}

所以尝试捕获const char * ans std :: string:

总是值得的
try {
   // stuff
}
catch( const char * s ) {
    cerr << s << endl;
}
catch( const std::string & s ) {
    cerr << s << endl;
}
// other catches here

答案 3 :(得分:0)

您还可以将我的工具用作外部调试器,捕获C ++异常并自动创建minidump文件以进行脱机调试。有关详细信息,请参阅http://alax.info/blog/1211

好处是您不需要任何代码,或者您甚至可以调试您没有代码的应用程序,或者您无法以任何理由重建它。转储文件将为您提供堆栈和信息,应用程序将能够继续执行。