Customizing uncaught exception messages

Yang Zhang yanghatespam@gmail.com
Thu May 15 16:01:00 GMT 2008


Joel Dice wrote:
> On Thu, 15 May 2008, Yang Zhang wrote:
> 
>> Hi, is it possible to get more informative error messages for uncaught 
>> exceptions?  For instance, I'm interested in getting Java-like 
>> tracebacks with file and line numbers.  Currently what I see is far 
>> from that, e.g.:
>>
>> terminate called after throwing an instance of 'std::logic_error'
>>  what():  basic_string::_S_construct NULL not valid
>>
>> Is there any way to hook into the exception handling mechanisms and 
>> produce such a traceback?  If the solution is platform-dependent, I am 
>> using GNU/Linux with gcc 4.2.3.
> 
> In cases where the exception is thrown by your own code, you may be able 
> to use backtrace() and backtrace_symbols(), declared in execinfo.h, to 
> capture the stack trace before unwinding.  Then use addr2line (a command 
> line utility included in binutils) to get the line numbers.

I guess my question boils down to: how do I do the first thing you 
mentioned - that is, cause all throws to trigger my own code (which e.g. 
calls backtrace())?  I've used backtrace() and addr2line before, but I 
just don't know enough about how C++ exceptions work.

-- 
Yang Zhang
http://www.mit.edu/~y_z/



More information about the Gcc-help mailing list