开发者

How to log an c++ exception

Do you know how can I log the exception ? right now the message in the catch statement is printed, but i cannot understood why ins´t Manage.Gere() called sussefully .

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 Ent开发者_运维百科er****** "); //It doesnt get printed

}


A well-behaved API should only throw objects of types derived from std::exception. If that's the case, then the exception will have a member function const char *what() containing a message, which will hopefully describe the error. So you could try this, and hope that it helps:

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

If it throws a type that isn't a std::exception, then you will need to look at the documentation and/or source for the function to see what could go wrong, and what types it does throw. If none of this is available, I would be looking for a better library.


First thing you need there is find which exceptions Manage.Gere can throw.
Then catch them specifically like catch(FirstExceptionGereThrows &exc) and when you catch all possible exceptions, you'll know what is failing in Manage.Gere.

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

After, and if you are lucky enough the exceptions thrown by Manage.Gere may include some extra info about the crash which you could log as well.

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


As others have suggested, RTFM is really the answer. However, I've found that lazy programmers often write code like:

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

so it is always worthwhile trying to catch both 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


You can also use my tool as an external debugger which catches C++ exceptions and automatically create minidump files for offline debugging. See http://alax.info/blog/1211 for details.

The good thing is that you don't need any code for this, or you can even debug an application which you don't have code for, or you cannot rebuild it for any reason. The dump file will get you stacks and information, and the application will be able to continue execution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜