I caught an exception! Yay! Now what?

Started by
3 comments, last by Servant of the Lord 11 years, 6 months ago
Sometimes my program catches exceptions from unknown locations, and the what() part of the std::exception doesn't do too much to help me.

Example: basic_string::_S_construct null not valid

Okay, so a string is be accidentally initialized with NULL somewhere (after googling that poorly worded message).

...

...

...?

Where was the exception thrown from? C++ doesn't have any way to give a strack-trace of the throw? Does C++11 add anything of use here?

I'm using MinGW on Windows 7 32bit. Some options, like Mr Edd's dbg library, only seem to support stacktraces if you are the one throwing the exception. But in this case, std::string is throwing it.

Note: Normally I try to catch the exceptions as close to the source as possible, if I know an exception is likely to be thrown in a certain area. However, in this case, it's being caught by my lowest "just catch everything" at the entry of my program.

Also note: I'm sure I can manually locate the cause of the exception within 15 minutes - that's not the issue. I am wondering if there is a better solution, for all future cases. than to manually locating it.
Advertisement
I'm not sure this really helps you, but Visual C++ allows you to set it to break on the line throwing any exception deriving from std::exception (as well as as lots of windows-specific exceptions).

Maybe gdb has something similar?
[size="1"]
In theory you can install a terminate handler with set_terminate() and call the stack walk in your terminate handler. However, terminate() is only called when you don't handle the exception; if you've got a catch all handler somewhere you'd be out of luck. On Linux you can hook gcc's __cxa_throw, but the way that linking works on Windows makes it much more difficult to do that. If you're willing to go over to the dark side, it's also pretty easy to get a stack trace for an exception on MSVC since it implements it's exceptions on top of SEH.

I'm not sure this really helps you, but Visual C++ allows you to set it to break on the line throwing any exception deriving from std::exception (as well as as lots of windows-specific exceptions).

Maybe gdb has something similar?

[font=courier new,courier,monospace](gdb) catch throw[/font]

Yeah, sounds stupid. Works.

Stephen M. Webb
Professional Free Software Developer

Wow, thanks Bregma - that's exactly what I needed. It found the flaw within 10 seconds.

For others using QtCreator, here's how you use it:
Right-click where your breakpoints are, and select 'Add breakpoint'

Then you get a nice few options:
addbreakpoint2012101810.png

This topic is closed to new replies.

Advertisement