捕获未知异常
在我的代码的发布版本中,有一行抛出异常,但我不知道它是什么类型的异常,因此我无法正确捕获它或找出问题。
我使用 catch(...) 但这几乎毫无价值。
这是一些伪代码
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");
}
那么,有什么方法可以获取有关抛出的未知异常的任何详细信息吗?只要有一种类型就太好了。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不一定,它可能是一个
int
、一个const char*
或一个RhubarbPie
超级智能指针。但是:
std::exception
。这将捕获很多 C++ 本机异常。(更新:MFC 显然使用指针的 throw-and-catch。只要您捕获抛出的内容,这也有效。)
Not really, it could be an
int
, aconst char*
or aRhubarbPie
über-smart pointer.However:
std::exception
too. That will catch a lot of C++ native exceptions.(Update: MFC apparently uses throw-and-catch by pointer. That works too, as long as you catch what is thrown.)
当您指定 MFC 的使用时,我将假设您正在使用 Visual Studio 的某个版本。如果是这种情况,并且您能够在调试模式下运行程序,那么您可以将调试器设置为在未处理的异常上中断。这需要删除代码中的
catch(...)
部分,但它应该在正确的位置中断到调试器,并为您提供有关异常本身的有用信息。我建议您参阅此处和此处。
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.
每个异常都应该派生自
std::exception
,然后您可以使用RTTI。标准 catch 块是在 c++0x 中,您可以使用 std::current_exception 并可能将 exception_ptr 传递到一些聪明的库中进行分析。
请记住,异常可以是内置函数和其他没有 RTTI 的类型,这就是为什么您应该始终从 std::exception 派生的原因。
Every exception should derive from
std::exception
, then you can use RTTI. Standard catch block isIn c++0x you can use
std::current_exception
and perhaps pass theexception_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.
不,这里不是。 catch( ...) 应该只用作最后的手段。
No here isn't. catch( ...) should only be used as a last resort really.
一种选择是不捕获错误并在调试器中运行编程器(这在发布模式下是可能的)。 Visual Studio 将中断发生未捕获异常的代码。
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.