views:

142

answers:

3

Is there any way to get at least someinformation inside of here?

...
catch(...)
{
  std::cerr<<"Unhandled exception"<<std::endl;
}

I have this as a last resort around all my code. Would it be better to let it crash, because then I at least could get a crash report?

+7  A: 

No, there isn't any way. Try making all your exception classes derive from one single class, like std::exception, and then catch that one.

You could rethrow in a nested try, though, in an attempt to figure out the type. But then you could aswell use a previous catch clause (and ... only as fall-back).

Johannes Schaub - litb
+1  A: 

Yes there is, but how useful it is is open to debate:

#include <exception>
#include <iostream>
using namespace std;

int f() {
    throw "message";
}

int main() {
    try {
     f();
    }
    catch ( ... ) {
     try {
      throw;
     }
     catch( const char *  s ) {
      cout << "caught " << s << endl;
     }
    }
}

And to actually to answer your question, IMHO you should always have a catch(...) at the top level of your code, that terminates (or otherwise handles) when presented with an unexpected exception in your application, in a manner fully documented by your application's manual.

anon
but this does not catch throw 7
Net Citizen
Possibly - but the original question was about extracting "some" information from a catch(...) and this is the only method I know for doing that.
anon
A: 

I believe you should catch (...), if you have a reasonable course of action at that point and want the application to keep running.

You don't have to crash in order to generate a crash report, mind you. There's API for generating a mini-dump and you can do it in your SEH handler.

Assaf Lavie