I have a function that allocates a text font on the heap from a filename. It looks like this:
std::unique_ptr<sf::Font> newFont(std::string&& fileName)
{
auto font = std::make_unique<sf::Font>();
if (!font->loadFromFile(fileName))
{
exit(0);
}
return font;
}
Someone on Stack Exchange - Code Review told me that I needed better error messages, since my program just silently exits if the file is not found. But since I'm writing a game, I'm not using a console window for output. I was thinking that maybe I could throw some kind of custom compiler error that triggers when the file is not found. Something like "Unable to allocate font: <font_path>". Is there a way to do this, or should I solve this some other way?
Exceptions are what you are looking for:
Now we can print error messages using a
try-catchblock:Normally you could implement
PRINT_ERRORby just printing tostd::cerr, but since you don't have a console to print to, you will need to do something else with the error message. Options include:std::ofstream