Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从catch块获取回溯跟踪_C++_Linux_Exception Handling_Backtrace - Fatal编程技术网

C++ 从catch块获取回溯跟踪

C++ 从catch块获取回溯跟踪,c++,linux,exception-handling,backtrace,C++,Linux,Exception Handling,Backtrace,我使用从引发异常的位置获取信息。在异常的构造函数中,我将回溯存储在std::string中,在该类型异常的catch块中,我将打印此回溯 但我想知道,对于其他异常类型,是否有可能在catch块中获得相同的回溯跟踪?我不这么认为。当executons在catch块中停止时,堆栈将展开,以前发生的所有事情都不再在堆栈中。您可能对正在开发的Boost库感兴趣:。例如: #include <boost/backtrace.hpp> #include <iostream> int

我使用从引发异常的位置获取信息。在异常的构造函数中,我将回溯存储在std::string中,在该类型异常的catch块中,我将打印此回溯


但我想知道,对于其他异常类型,是否有可能在catch块中获得相同的回溯跟踪?

我不这么认为。当executons在catch块中停止时,堆栈将展开,以前发生的所有事情都不再在堆栈中。

您可能对正在开发的Boost库感兴趣:。例如:

#include <boost/backtrace.hpp>
#include <iostream>

int foo()
{
    throw boost::runtime_error("My Error");
    return 10;
}

int bar()
{
    return foo()+20;
}


int main()
{
    try {
        std::cout << bar() << std::endl;
    }
    catch(std::exception const &e)
    {
        std::cerr << e.what() << std::endl;
        std::cerr << boost::trace(e);
    }
}

希望这有帮助

有问题的类是否共享一个可编辑的公共基


除此之外,我还提供了一个精彩但却被低估的答案-P有些人也这么认为。

但我不是linux,因此这里没有visual studio;)是的,我抛出的异常类有一个共同的基础,我可以得到这些异常类的回溯。问题是如何从其他异常类型获取回溯?例如std::out_of_range@VJo我的脏黑客不是Visual C++特定的(实际上,我只在GCC中测试过),但是它也不适合于严肃的生产使用。对于std::out_of_范围,这是一个麻烦-猜测这取决于它对您的价值,但是您可以使用调试器技术探索更改std::exception本身。您是在处理从第三方库抛出的std::out_of_range等(如果是,您甚至有源代码),还是在您自己的代码中?有太多的源代码来更改throw语句吗?我不能使用它,因为只有当异常从同一个编译单元(cpp文件)抛出时,它才起作用。一旦抛出异常的函数在另一个文件中,它将无法工作。是的,这正是我需要的,但据我所知,它仍在boost版本中,目前我被锁定在boost 1.34.1中:(也许你可以单独下载并单独使用它。然后当/如果它成为正式的Boost时,你可能可以切换/升级。祝你好运!抱歉,但我意识到这并不能回答我的问题。为此,你必须抛出Boost异常,但如果捕获到另一种异常类型,则不会打印回溯。没问题。希望如此。)你找到了你要找的东西。是的,没错。这让我们想到“当抛出异常时,我该怎么做?”-Len Holgate提到了调试API,以便在抛出异常时准确捕获异常。看起来很复杂,但可行。您可能需要仔细查看。它可能会让您接近您想要的位置。std::set_terminate的问题是,调用回调时程序仍将终止。
My Error
0x403fe1: boost::stack_trace::trace(void**, int) + 0x1b in ./test_backtrace
0x405451: boost::backtrace::backtrace(unsigned long) + 0x65 in ./test_backtrace
0x4054d2: boost::runtime_error::runtime_error(std::string const&) + 0x32 in ./test_backtrace
0x40417e: foo() + 0x44 in ./test_backtrace
0x40425c: bar() + 0x9 in ./test_backtrace
0x404271: main + 0x10 in ./test_backtrace
0x7fd612ecd1a6: __libc_start_main + 0xe6 in /lib/libc.so.6
0x403b39: __gxx_personality_v0 + 0x99 in ./test_backtrace