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 exc开发者_Python百科eption? Just a type would be great.
thanks
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.
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.
Every exception should derive from std::exception
, then you can use RTTI. Standard catch block is
catch (const std :: exception & e) {
// e .what ();
// typeid (e);
}
catch (...) {
// WTF ?!?!?
}
In c++0x you can use std::current_exception
and perhaps pass the exception_ptr
into some clever library for analysis.
Bear in mind that exceptions can be buildins and other types which have no RTTI, which is why you should always derive from std::exception.
No here isn't. catch( ...) should only be used as a last resort really.
One option is to not catch the error and run the programmer in the debugger (this is possible in Release mode). Visual Studio will break into the code where the uncaught exception occurs.
精彩评论