Sign in to follow this  

I caught an exception! Yay! Now what?

This topic is 1916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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: [i]basic_string::_S_construct null not valid[/i]

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 [url="http://www.mr-edd.co.uk/code/dbg"]dbg[/url] 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 '[b]Add breakpoint[/b]'

Then you get a nice few options:
[img]http://img7.imageshack.us/img7/1972/addbreakpoint2012101810.png[/img]

Share this post


Link to post
Share on other sites

This topic is 1916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this