简体   繁体   English

如何知道捕获了什么,同时捕获所有 C++ 异常

[英]How to know what did catch, while catching all C++ exceptions

One of the MFC function throwing an exception that cannot catch by std::exception or CException. MFC function 之一抛出无法被 std::exception 或 CException 捕获的异常。 So for identify the issue, I'm going to catch all exception as the last way (only to know what goes wrong, not for production).因此,为了确定问题,我将以最后一种方式捕获所有异常(只知道出了什么问题,而不是用于生产)。

try
{
  CPropertySheet::OnInitDialog();
}
catch (...)
{
  //need to know what did catch.
}

But is there a way to know what did catch after catching all the exception in C++?但是有没有办法知道在C++中捕获所有异常后捕获了什么? At-lease the error message of the exception or any other clue.保留异常的错误消息或任何其他线索。

Short answer: no. 简短的回答:不。

catch(...) catches everything without regards for the type of what was thrown. catch(...)捕获所有内容,而无需考虑所抛出的类型。 You could even throw an int and it would be caught. 您甚至可以抛出一个int ,它将被捕获。 That also means you can't get an error message, because there might be none, like in the case of an int . 这也意味着您不会收到错误消息,因为可能没有错误消息,例如int的情况。

To solve your problem, you can only catch any exception type that might be thrown by the code in question, according to its documentation, but without the catch-all handler. 为了解决您的问题,根据其文档,您只能捕获所讨论的代码可能抛出的任何异常类型,而不能使用全部捕获处理程序。 If you still get an uncaught exception there might be some exceptions thrown by the runtime. 如果仍然遇到未捕获的异常,则运行时可能会抛出一些异常。 You'll have to refer to your compiler's documentation to know which that may be. 您必须参考编译器的文档才能知道可能是哪种。 For example, in some runtimes an access violation (eg null ponter access) may be treated as exceptions that can be caught. 例如,在某些运行时中,访问冲突(例如,空ponter访问)可能被视为可以捕获的异常。

Another option would be to use a debugger and configure it to break when an exception is thrown. 另一种选择是使用调试器,并将其配置为在引发异常时中断。 It can show you the exact location and the type of exception that is thrown. 它可以为您显示确切的位置和引发的异常类型。 As you say it happens only in release build, you might try to use a release build with debug infos. 正如您所说的那样,这仅在发行版本中发生,您可以尝试将发行版本与调试信息一起使用。

The ... is for a last resort. ...是不得已而为之。
But "most" exceptions in C++ are derived from std::exception or a class that is derived from this base class. Though it is not required "most" projects conform to this standard.但是 C++ 中的“大多数”异常是从std::exception或从该基数 class 派生的 class 派生的。虽然不需要“大多数”项目符合此标准。

So you can achieve what you want by adding a catch for std::exception.所以你可以通过为 std::exception 添加一个 catch 来实现你想要的。

try
{
  CPropertySheet::OnInitDialog();
}
catch (std::exception const& e)
{
    std::cerr << "Exception: " << e.what() << "\n";
    throw;
}
catch (...)
{
    std::cerr << "Exception: >UNKNOWN<\n";
    throw;
}

Also some OS will also give you some help if the exception escapes main().如果异常从 main() 中逃逸,一些操作系统也会给你一些帮助。 So rethrow the exception after generating an error message may help you track down the error.因此,在生成错误消息后重新抛出异常可能会帮助您追踪错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM