C ++获取std :: out_of_range异常的位置

时间:2018-03-29 05:44:20

标签: c++ debugging exception-handling

我正在制定一个相当冗长的计划,经过一段时间的运行,我突然得到了:

php_value auto_prepend_file "/path_to_file/file.php"

作为异常处理的新手,我做了一些研究,发现我可能会通过在主函数中添加以下内容来获取更多信息:

terminate called after throwing an instance of 'std::out_of_range'
 what(): basic_string::substr

结果如下:

int main(int argc, char **argv){
    try{
        //stuff
    }
    catch(exception const &exc){
        cerr << "Caught exception: " << exc.what() << endl;
    }
}

这不比默认输出更有用;它没有告诉我任何关于触发核心转储的行(我的程序中有很多substr调用),substr试图处理的数据等等。有没有一种方法可以在C ++中显示这样的信息,或者是我唯一选择使用调试器,如gdb?

1 个答案:

答案 0 :(得分:1)

有几种方法。

  1. 正如您所说,一个调试器 - 但是一旦代码投入生产,这对您无济于事。

  2. 嵌套异常和函数try块。 e.g:

  3. #include <exception>
    #include <stdexcept>
    #include <iostream>
    #include <sstream>
    #include <iomanip>
    
    void bar(std::string& s, int i)
    try
    {
        s.at(i) = 'A';
    }
    catch(...)
    {
        std::ostringstream ss;
        ss << "error in bar(" << std::quoted(s) << ", " << i << ")";
        std::throw_with_nested(std::runtime_error(ss.str()));
    }
    
    void foo(std::string& s)
    try
    {
        bar(s, 6);
    }
    catch(...)
    {
        std::ostringstream ss;
        ss << "error in foo(" << std::quoted(s) << ")";
        std::throw_with_nested(std::runtime_error(ss.str()));
    }
    
    void stuff()
    try
    {
        std::string s;
        foo(s);
    }
    catch(...)
    {
        std::throw_with_nested(std::runtime_error("error in stuff()"));
    }
    
    void print_exception(std::ostream& os, const std::exception& e, int level =  0)
    {
        os << std::string(level, ' ') << "exception: " << e.what() << '\n';
        try {
            std::rethrow_if_nested(e);
        } catch(const std::exception& e) {
            print_exception(os, e, level+1);
        } catch(...) {}
    }
    
    int main()
    {
        try{
            stuff();
        }
        catch(std::exception& e)
        {
            print_exception(std::cerr, e);
            return 127;
        }
        return 0;
    }
    

    示例输出:

    exception: error in stuff()
     exception: error in foo("")
      exception: error in bar("", 6)
       exception: basic_string::at: __n (which is 6) >= this->size() (which is 0)
    
    1. 您可以使用boost::stacktrace代替上述嵌套异常处理。
    2. http://coliru.stacked-crooked.com/a/f21bd35632a0a036

相关问题