Sign in to follow this  
MatthewDiaz

Exception Handling and Fatal Errors

Recommended Posts

MatthewDiaz    166
I am working on my first serious 2D platformer for PC with Directx11, and Microsoft Visual C++. I have some exception handling done but not all. I have nothing yet for fatal errors. For example when loading resources I don't check to see if the resource is there, so if the file is renamed, moved or deleted the program probably will crash. Now I decided to simply use my own system to handle this. I'll check to see if the file is there before it is loaded and if it isn't I'll call a function that shows a messagebox with the error code and explanation of what caused it and any button will shut down the program. Now is this ok? I don't see how I could recover the program with a necessary missing file. Unless I had a backup folder with all necessary resources and moved the missing file back to where it is missing? Why go through all of that if the player shouldn't be tampering with such files anyway correct?


Also a general question regarding exception handling. Let's say a integer needs to be between 1 and 10 for the program to function properly. Now if I am sure that the program is set up in a way to where this integer won't have a value outside of its boundaries is an exception check even neccessary? Is it possible for a freak thing to happen and the variable wind up having a wrong value for some reason outside of the program's control?

Share this post


Link to post
Share on other sites
Washu    7829
A missing texture? Use your debug placeholder. A missing model? Use a debug placeholder.

Common examples of this is that many games have a magenta texture or the like which appears nowhere except where textures are missing. For the model its often a large cube or similar object that will never appear in game. Again, its just a placeholder.

These are common fallbacks, which you will need during development at the least, while you get art assets sorted out.

Only when you have no other recourse should you CtD.

Share this post


Link to post
Share on other sites
MatthewDiaz    166
Wow that is actually a good idea for textures. Nonetheless though, the player would be inclined to reinstall or repair whether the game crashes or if it continues with single color textures right? What I am asking I guess is do I have to have the program recover from a missing texture?

Share this post


Link to post
Share on other sites
Washu    7829
Yes, you SHOULD program to recover from a missing texture, you could report the error to the player at the least, and at best you could download a patch online to fix the issue.

"Warning: Data files necessary for running this game are unavailable. Should an attempt be made to repair the game?" [Y/N]

Share this post


Link to post
Share on other sites
SiCrane    11839
Yes, it's possible for numbers to change at random. It can happen when dealing with hardware failure/overclocked processors and it can also happen if someone is trying to cheat at your game and is rewriting memory. It can also happen if you make a mistake and dereference a bad pointer or overrun a buffer.

Share this post


Link to post
Share on other sites
rip-off    10976
For your second question, if something very unexpected occurs, like SiCrane's list, then you pretty much cannot depend on anything. If you expect the value is valid, then use an assertion for Debug mode, and cross your fingers in the shipping code. Don't use exceptions for things that should not occur. Exceptions are for [i]possible but improbable situations[/i], not for "impossible"(assertion in Debug, all bets are off in Release) or probable situations (error codes or fallback behaviour).

Share this post


Link to post
Share on other sites
MatthewDiaz    166
Makes sense. I mean a good example is what I am doing right now. I am using my own textures for the font and box for my errorbox. Now I simply load my errorbox texture file and font texture atlas anytime an error box needs to appear. All of a sudden I thought ok, well what happens if those two texture files are not there or the other two files that are needed to load this particular font properly? Do I do an exception check inside my error code and call it again with a different font and textbox if those other two aren't there? Lol. i was driving myself crazy. If I do this my way then I have to hope no one ever tampers with those specific files.

Share this post


Link to post
Share on other sites
c_olin    197
Based on your last post it sounds like you might be loading resources manually as different parts of your code needs them. I would recommend you look into writing a centralized resource cache to abstract away the IO details (and error handling).

Here is an example of how mine works:

[source lang="cpp"]
ResourceCache resourceCache("Data.zip");
shared_ptr<mesh> someMesh = resourceCache.get<mesh>("Test.mesh");
shared_ptr<mesh> someMeshAgain = resourceCache.get<mesh>("Test.mesh"); // The mesh is not loaded again. A pointer to the cached mesh is returned.
shared_ptr<textuer> someTexture = resourceCache.get<texture>("DoesNotExist.png"); // The texture does not exist. A pointer to the default texture is returned.
[/source]

Notice how no exception handling is needed when retrieving resources. And with the raw IO abstracted away the code doesn't care if the files are on the filesystem, in a zip file, over a network, etc...

Since the IO is abstract you can also have resources existing in different sources. For example, you can have core resources built with the executable so that they will not fail to load unless someone messes with the executable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this