用GDB中更有意义的错误描述替换SIGABRT

Replacing SIGABRT with more meaningful description of error in GDB

本文关键字:描述 替换 SIGABRT 错误 GDB 更有意义      更新时间:2023-10-16

当我在gdb调试器中执行反向跟踪(bt)时,它显示程序接收到信号SIGABRT, Aborted.

在我写的c++程序中,我只写了throw;

是否有一种方法可以用更有意义的错误描述替换SIGABRT ?

index out of bound error,甚至index 15 exceeds the bounds of the array with shape (2, 3, 4).

我不认为改变信号是可能的,但是如果你想在gdb中看到一个更有用的错误消息,你总是可以抛出一个特定的异常:

void do_something(){
    throw std::out_of_range ("This is a very specific message.");
}

在gdb中显示如下:

(gdb) r
Starting program: /home/pascal/code/exception-test 
terminate called after throwing an instance of 'std::out_of_range'
  what():  This is a very specific message.
Program received signal SIGABRT, Aborted.
0x00007ffff753c035 in raise () from /lib/x86_64-linux-gnu/libc.so.6

您可以在这里找到可能的异常列表:http://en.cppreference.com/w/cpp/error/exception