Exception catching question

I am having trouble setting up the following to successfully catch an exception (and more importantly not crash my program) when a bad file name is passed. Here is the function that isn't working...

All the variables are declared correctly elsewhere. If a good name is passed, the file loads successfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
void loadFile(std::string fileName) {
	try {
	_img = al_load_bitmap(fileName.c_str());
	setWidth(al_get_bitmap_width(_img));
	setHeight(al_get_bitmap_height(_img));
	setBorderColor(Color.BLACK);
	setFillColor(Color.BLACK);
	setFilled(false);
	}
	catch (const std::exception &e) {
		std::cerr << e.what();
	}
}


To add a bit more clarity, this function is built to work with Allegro 5. Also, the setWidth() and setHeight() functions break if a file does not load successfully since they assume a bitmap has been 'created' into memory.
Last edited on
Allegro is written in C so it doesn't throw exceptions.

If you read the documentation for al_load_bitmap you see that it returns a null pointer when an error occur.

https://www.allegro.cc/manual/5/al_load_bitmap
Topic archived. No new replies allowed.