捕获未知异常

发布于 2024-11-19 06:00:06 字数 533 浏览 2 评论 0原文

在我的代码的发布版本中,有一行抛出异常,但我不知道它是什么类型的异常,因此我无法正确捕获它或找出问题。

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心不设防 2024-11-26 06:00:06

不一定,它可能是一个 int、一个 const char* 或一个 RhubarbPie 超级智能指针。

但是:

  • 也尝试捕获 std::exception 。这将捕获很多 C++ 本机异常。
  • 您的异常可能是 .NET 异常,因此请尝试捕获该异常,而不是 MFC Base 异常。 (看起来您正在执行 C++/CLI。在这种情况下,.NET 异常最终会出现在 catch-all 子句中)
  • 此外,异常通常意味着通过 C++ 中的引用来捕获
    更新:MFC 显然使用指针的 throw-and-catch。只要您捕获抛出的内容,这也有效。
  • 使用 __try 和 __catch 也可能有所帮助,因为某些“硬件”堆栈溢出、访问冲突等异常在 Windows 上也是未知异常。捕获它们的语法有点不同,但您会得到一个异常标识符,可用于报告抛出的异常类型。我用它来打印应用程序中致命错误的堆栈跟踪。

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.
紫﹏色ふ单纯 2024-11-26 06:00:06

当您指定 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.

花期渐远 2024-11-26 06:00:06

每个异常都应该派生自std::exception,然后您可以使用RTTI。标准 catch 块是

catch (const std :: exception & e) {
    // e .what ();
    // typeid (e);
}
catch (...) {
    // WTF ?!?!?
}

在 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 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.

大姐,你呐 2024-11-26 06:00:06

不,这里不是。 catch( ...) 应该只用作最后的手段。

No here isn't. catch( ...) should only be used as a last resort really.

像你 2024-11-26 06:00:06

一种选择是捕获错误并在调试器中运行编程器(这在发布模式下是可能的)。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文