| | |
exceptions fall back to std::exception
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I have a child of std::exception defined as:
(I would normally have what() return message.c_str(), but I've done it like this for debugging reasons).
It's thrown here:
and an atempt to catch it:
this section of the code prints "std::exception". the exception that is then still being thrown causes the program to exit with:
adding
c++ Syntax (Toggle Plain Text)
struct image_load_failed : public std::exception { image_load_failed(std::string const s) : message(s) {} virtual ~image_load_failed() throw() {} const char* what() const throw() { return "image load failed";//message.c_str(); } std::string const message; };
It's thrown here:
c++ Syntax (Toggle Plain Text)
throw image_load_failed(IMG_GetError());
and an atempt to catch it:
c++ Syntax (Toggle Plain Text)
try { circle.loadImage("circle.bmp", Color(255,255,255)); } catch(Surface::image_load_failed const& e) { std::cout << e.what() << std::endl; throw e; } catch(std::exception const& e) { std::cout << e.what() << std::endl; throw e; }
C++ Syntax (Toggle Plain Text)
terminate called after throwing an instance of 'std::exception' 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...".
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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?
hmm.... well, i defined a function:
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
c++ Syntax (Toggle Plain Text)
void func() { throw Surface::image_load_failed("my error"); }
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...".
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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).
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).
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
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
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]
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
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
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
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...".
![]() |
Other Threads in the C++ Forum
- Previous Thread: Code mess..
- Next Thread: Converting digits..need help!
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





