Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catching unknown exception

In the release version of my code one line is throwing an exception and I don't know what type of exception it is so I can't catch it correctly or figure out the problem.

I using the catch(...) but that's pretty much worthless.

here is some pseudo code

 try
  {
    m_mmwIPC = gcnew NiftyIPC(gcnew String("Monitor"), true);
  }
  catch (CException* e)
  {
    TCHAR   szCause[255];
    e->GetErrorMessage(szCause, 255);
    CString errorStr = szCause;
    RemoveLineFeeds(errorStr);
    OutputDebugString(errorStr);
  }
  catch(...)
  {
    OutputDebugString(L"Unknown exception\n");
  }

So, is there any way to get any details on the thrown unknown exception? Just a type would be great.

thanks

like image 607
Iunknown Avatar asked Jul 08 '11 13:07

Iunknown


2 Answers

Not really, it could be an int, a const char* or a RhubarbPie über-smart pointer.

However:

  • Try catching std::exception too. That will catch a lot of C++ native exceptions.
  • Your exception is probably a .NET one, so try to catch that one, not the MFC Base exception. (It looks like you're doing C++/CLI. In that case, .NET-exceptions end up in the catch-all clause)
  • Also, exceptions are usually meant to be caught by reference in C++ (Update: MFC apparently uses throw-and-catch by pointer. That works too, as long as you catch what is thrown.)
  • It might also help to use __try and __catch, since some "hardware" exceptions like stack-overflow, access violation, etc, are also unknown exceptions on Windows. The syntax for catching them are a bit differently, but you get an exception identifier that can be used to report the type of exception thrown. I use that to print stack-traces on fatal errors in our apps.
like image 113
Macke Avatar answered Oct 25 '22 09:10

Macke


As you specify the use of MFC, then I will make the assumption that you're working with a version of Visual Studio. If this is the case and you are able to run your program in debug mode, then you can set the debugger to break on unhandled exceptions. This would require removing the catch(...) part of your code, but it should break into the debugger at the correct point, and provide you with useful information on the exception itself.

I refer you to the Microsoft documentation here and here.

like image 30
icabod Avatar answered Oct 25 '22 08:10

icabod