Jump to content

  • Log In with Google      Sign In   
  • Create Account


I caught an exception! Yay! Now what?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
1Likes
Like

Posted 16 October 2012 - 03:55 PM

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.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


Sponsor:

#2 mrbastard   Members   -  Reputation: 1573

Like
1Likes
Like

Posted 16 October 2012 - 04:08 PM

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?


#3 SiCrane   Moderators   -  Reputation: 9418

Like
2Likes
Like

Posted 16 October 2012 - 04:27 PM

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.

#4 Bregma   Crossbones+   -  Reputation: 4770

Like
5Likes
Like

Posted 16 October 2012 - 06:41 PM

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?

(gdb) catch throw

Yeah, sounds stupid. Works.
Stephen M. Webb
Professional Free Software Developer

#5 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
3Likes
Like

Posted 18 October 2012 - 09:56 AM

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:
Posted Image

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS