I have a child of std::exception defined as:

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;
	};

(I would normally have what() return message.c_str(), but I've done it like this for debugging reasons).

It's thrown here:

throw image_load_failed(IMG_GetError());

and an atempt to catch it:

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;
	}

this section of the code prints "std::exception". the exception that is then still being thrown causes the program to exit with:

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)

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:

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....

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).

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]

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.