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

时间:2022-09-06 20:10:23

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

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

5 个解决方案

#1


36  

If it derives from std::exception you can catch by reference:

如果它源自std::异常,可以通过引用捕获:

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::string or some_library_exception_base).

但是,如果异常是一些没有从std:::exception派生的类,那么您必须提前知道它的类型(例如,您是否应该捕获std: string或some_library_exception_base)。

You can do a catch all:

你可以做一件事:

try
{
}
catch (...)
{
}

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

但是,你不能做任何有例外的事情。

#2


12  

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

#3


3  

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

如果您对gcc或CLANG使用ABI,您可以知道未知的异常类型。但它不是标准解。

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

看到https://*.com/a/24997351/1859469

#4


1  

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)在某些系统中,当执行抛出语句时,编译器生成对(无文档说明的?)函数的调用。要找出系统的函数,编写一个简单的hello world程序,抛出并捕获异常。启动调试器并在exception构造函数中放置断点,并查看调用它的位置。caling函数可能类似于__throw()。然后,使用要调查的程序作为debuggee重新启动调试器。在上面提到的函数(__throw或其他)中设置断点并运行程序。当抛出异常时,调试器会停止,您就在那里找出原因。

#5


1  

Inspired by Dawid Drozd answer:

受到Dawid Drozd回答的启发:

include this header #include <exception>

包括标题#include

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();
    }
}

#1


36  

If it derives from std::exception you can catch by reference:

如果它源自std::异常,可以通过引用捕获:

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::string or some_library_exception_base).

但是,如果异常是一些没有从std:::exception派生的类,那么您必须提前知道它的类型(例如,您是否应该捕获std: string或some_library_exception_base)。

You can do a catch all:

你可以做一件事:

try
{
}
catch (...)
{
}

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

但是,你不能做任何有例外的事情。

#2


12  

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

#3


3  

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

如果您对gcc或CLANG使用ABI,您可以知道未知的异常类型。但它不是标准解。

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

看到https://*.com/a/24997351/1859469

#4


1  

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)在某些系统中,当执行抛出语句时,编译器生成对(无文档说明的?)函数的调用。要找出系统的函数,编写一个简单的hello world程序,抛出并捕获异常。启动调试器并在exception构造函数中放置断点,并查看调用它的位置。caling函数可能类似于__throw()。然后,使用要调查的程序作为debuggee重新启动调试器。在上面提到的函数(__throw或其他)中设置断点并运行程序。当抛出异常时,调试器会停止,您就在那里找出原因。

#5


1  

Inspired by Dawid Drozd answer:

受到Dawid Drozd回答的启发:

include this header #include <exception>

包括标题#include

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();
    }
}