exceptions fall back to std::exception

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

exceptions fall back to std::exception

 
0
  #1
Jan 7th, 2009
I have a child of std::exception defined as:
  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:
  1. throw image_load_failed(IMG_GetError());

and an atempt to catch it:
  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:
  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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: exceptions fall back to std::exception

 
0
  #2
Jan 7th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: exceptions fall back to std::exception

 
0
  #3
Jan 9th, 2009
hmm.... well, i defined a function:
  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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: exceptions fall back to std::exception

 
0
  #4
Jan 9th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: exceptions fall back to std::exception

 
0
  #5
Jan 9th, 2009
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
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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: exceptions fall back to std::exception

 
0
  #6
Jan 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: exceptions fall back to std::exception

 
0
  #7
Jan 10th, 2009
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC