views:

268

answers:

3

Hello!

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 Enter****** "); //It doesnt get printed

}
A: 

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";
}
Arkaitz Jimenez
How can i find which exceptions Manage.Gere can throw ?Tks
Reversed
Documentation or looking into Manage.Gere code, there is no other way.
Arkaitz Jimenez
If the exceptions are derived from std::exception, there is no absolute need to catch them specifically - just catch std::exception, and examine its what() member.
anon
Logic says most of them should derive from std::exception, but my experience says they rarely are, at least the libs I use.
Arkaitz Jimenez
+2  A: 

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.

Mike Seymour
just tried your code and in the log showed "Manage.Gere threw unknown exception"
Reversed
Then whatever was thrown wasn't a subclass of `std::exception`... maybe it's a user-defined or library-defined class or a basic type like `const char*`?
Mike DeSimone
Mike Seymour
And maybe `const CString*`, in case the API is truly evil.
Mike Seymour
A: 

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
anon
In addition to strings I find lots of 'throw int(errorCode)' so it is worth trying to catch these as well.
Martin York