C++ 如何捕获未知异常并打印它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3074646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:00:06  来源:igfitidea点击:

how to catch unknown exception and print it

c++exception

提问by helloWorld

I have some program and everytime I run it, it throws exception and I don't know how to check what exactly it throws, so my question is, is it possible to catch exception and print it (I found rows which throws exception) thanks in advance

我有一些程序,每次运行它时,它都会抛出异常,我不知道如何检查它到底抛出了什么,所以我的问题是,是否可以捕获异常并打印它(我找到了抛出异常的行)谢谢提前

回答by R Samuel Klatchko

If it derives from std::exceptionyou can catch by reference:

如果它源自std::exception您可以通过引用捕获:

try
{
    // code that could cause exception
}
catch (const std::exception &exc)
{
    // catch anything thrown within try block that derives from std::exception
    std::cerr << exc.what();
}

But if the exception is some class that has is not derived from std::exception, you will have to know ahead of time it's type (i.e. should you catch std::stringor some_library_exception_base).

但是,如果异常是某个不是从 派生的类,则std::exception您必须提前知道它的类型(即您是否应该捕获std::stringsome_library_exception_base)。

You can do a catch all:

你可以一网打尽:

try
{
}
catch (...)
{
}

but then you can't do anything with the exception.

但除此之外你什么也做不了。

回答by Dawid Drozd

In C++11 you have: std::current_exception

在 C++11 中,你有:std::current_exception

Example code from site:

来自站点的示例代码:

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>

void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try {
        if (eptr) {
            std::rethrow_exception(eptr);
        }
    } catch(const std::exception& e) {
        std::cout << "Caught exception \"" << e.what() << "\"\n";
    }
}

int main()
{
    std::exception_ptr eptr;
    try {
        std::string().at(1); // this generates an std::out_of_range
    } catch(...) {
        eptr = std::current_exception(); // capture
    }
    handle_eptr(eptr);
} // destructor for std::out_of_range called here, when the eptr is destructed

回答by Grzegorz Wolszczak

If you use ABI for gcc or CLANG you can know the unknown exception type. But it is non standard solution.

如果您将 ABI 用于 gcc 或 CLANG,您可以知道未知的异常类型。但它是非标准解决方案。

See here https://stackoverflow.com/a/24997351/1859469

请参阅此处 https://stackoverflow.com/a/24997351/1859469

回答by Axel

Try as suggested by R Samuel Klatchko first. If that doesn't help, there's something else that might help:

首先按照 R Samuel Klatchko 的建议尝试。如果这没有帮助,还有其他可能有帮助的东西:

a) Place a breakpoint on the exception type (handled or unhandled) if your debugger supports it.

a) 如果您的调试器支持,则在异常类型(已处理或未处理)上放置断点。

b) On some systems, the compiler generates a call to an (undocumented?) function when a throw statement is executed. to find out, what function that is for your system, write a simple hello world program, that throws and catches an exception. start a debugger and place a breakpoint in the exceptions constructor, and see from where it is being called. the caling function is probably something like __throw(). afterwards, start the debugger again with the program you want to investigate as debuggee. place breakpoint on the function mentioned above (__throw or whatever) and run the program. when the exception is thrown, the debugger stops and you are right there to find out why.

b) 在某些系统上,当执行 throw 语句时,编译器会生成对(未记录的?)函数的调用。要找出适合您系统的函数,请编写一个简单的 hello world 程序,该程序会抛出并捕获异常。启动调试器并在异常构造函数中放置一个断点,并查看从何处调用它。校准函数可能类似于 __throw()。之后,使用您要作为调试对象调查的程序再次启动调试器。在上面提到的函数(__throw 或其他)上放置断点并运行程序。当抛出异常时,调试器会停止,您可以立即找出原因。

回答by hamaney

Inspired by Dawid Drozd answer:

灵感来自 Dawid Drozd 回答:

#include <exception>
try
{
    // The code that could throw
}
catch(...)
{
    auto expPtr = std::current_exception();

    try
    {
        if(expPtr) std::rethrow_exception(expPtr);
    }
    catch(const std::exception& e) //it would not work if you pass by value
    {
        std::cout << e.what();
    }
}

回答by Lyndon Bauto

Inspired by hamaney answer:

灵感来自哈马尼答案:

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>

int main()
{
   try
   { 
      // Your code
   }
   catch (...)
   {
      try
      {
         std::exception_ptr curr_excp;
         if (curr_excp = std::current_exception())
         {
            std::rethrow_exception(curr_excp);
         }
      }
      catch (const std::exception& e)
      {
         std::cout << e.what();
      }
   }
}