943,809 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3576
  • C++ RSS
Jan 7th, 2009
0

exceptions fall back to std::exception

Expand Post »
I have a child of std::exception defined as:
c++ Syntax (Toggle Plain Text)
  1. struct image_load_failed : public std::exception
  2. {
  3. image_load_failed(std::string const s) : message(s) {}
  4. virtual ~image_load_failed() throw() {}
  5.  
  6. const char* what() const throw()
  7. {
  8. return "image load failed";//message.c_str();
  9. }
  10.  
  11. std::string const message;
  12. };
(I would normally have what() return message.c_str(), but I've done it like this for debugging reasons).

It's thrown here:
c++ Syntax (Toggle Plain Text)
  1. throw image_load_failed(IMG_GetError());

and an atempt to catch it:
c++ Syntax (Toggle Plain Text)
  1. try
  2. {
  3. circle.loadImage("circle.bmp", Color(255,255,255));
  4. }
  5. catch(Surface::image_load_failed const& e)
  6. {
  7. std::cout << e.what() << std::endl;
  8. throw e;
  9. }
  10. catch(std::exception const& e)
  11. {
  12. std::cout << e.what() << std::endl;
  13. throw e;
  14. }
this section of the code prints "std::exception". the exception that is then still being thrown causes the program to exit with:
C++ Syntax (Toggle Plain Text)
  1. terminate called after throwing an instance of 'std::exception'
  2. what(): std::exception

adding throw(Surface::image_load_failed) to the functions doesn't help. Anyone know why my classes are being interpreted as std::exceptions, and not the child classes I've created? (or more importantly, why the message is being lost)
Last edited by CoolGamer48; Jan 7th, 2009 at 9:12 pm.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jan 7th, 2009
0

Re: exceptions fall back to std::exception

Hmmm the only thing I can think of is...are you sure that you are catching the exception that you think you are? Maybe there's an internal one being thrown caused by trying to load the invalid image. I've never overloaded an exception myself, so this might be a dumb question, but why did you include a throw() call in your dtor and what() declarations?
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jan 9th, 2009
0

Re: exceptions fall back to std::exception

hmm.... well, i defined a function:
c++ Syntax (Toggle Plain Text)
  1. void func()
  2. {
  3. throw Surface::image_load_failed("my error");
  4. }

and called that from my try block, which resulted in the output I would have expected originally. I'll look more into the possibility that it is some other exception.

edit:
well, changing the body of Surface::loadImage() to throw image_load_failed("lol error"); didn't fix my original problem. that's odd... it seems to have something to do with the class....
Last edited by CoolGamer48; Jan 9th, 2009 at 6:26 pm.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jan 9th, 2009
0

Re: exceptions fall back to std::exception

My guess is that IMG_GetError() is throwing an exception.

However, it's hard to say, as you haven't provided a complete example. You've paraphrased where your think the problem is.

Try providing a small but complete example that illustrates your problem (with a main() function, and everything someone else would need to be able to get the same results you are).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 9th, 2009
0

Re: exceptions fall back to std::exception

This isn't something simple, i.e. that you output nothing in what() and
then you are double throwing the exception.

You are always going to get
Quote ...
terminate called after throwing an instance of 'std::exception'
what(): std::exception
with this code, it is just a question of what you are going to get first.

you can test that by getting rid of the throw e; (on the second catch) and replacing it with exit(1) [if you need to exit]
Last edited by StuXYZ; Jan 9th, 2009 at 7:02 pm.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Jan 9th, 2009
0

Re: exceptions fall back to std::exception

Here's an idea. Create a test function that only throws an exception of your custom type. Create its argument via its ctor, then call that function and try to catch your exception. If it is caught, your derived exception is fine, and it's an exception thrown elsewhere. And vice versa if it isnt caught.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Jan 10th, 2009
0

Re: exceptions fall back to std::exception

Ahh.... I figured it out. Somewhere in the methods of my class I was catching the exception that would normally be thrown as an std::exception and re-throwing it (not really sure why I had done that...). Once I removed that the exceptions work as expected.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Code mess..
Next Thread in C++ Forum Timeline: Converting digits..need help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC