【问题标题】:C++ Getting Location of std::out_of_range ExceptionsC++ 获取 std::out_of_range 异常的位置
【发布时间】:2018-03-29 05:44:19
【问题描述】:

我正在编写一个相当长的程序,在运行了一段时间后,我突然得到:

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

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

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

结果如下:

Caught exception: basic_string::substr

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

【问题讨论】:

  • 使用调试器有什么问题。然后它可以准确地告诉你是哪一行引发了异常......

标签: c++ debugging exception-handling


【解决方案1】:

有几种方法。

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

  2. 嵌套异常和函数尝试块。例如:

 

#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 代替上述嵌套异常处理。

http://coliru.stacked-crooked.com/a/f21bd35632a0a036

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多