PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Error Handling

Hi All,

I am starting to realise the I should implement some error handling in my code but am not sure the best way to do this. At the moment, all I am doing is displaying an MessageBox when an error occurs. Unfortunately, this approach does little in terms of allowing the application to die gracefully.

Does anyone have any suggestions for a good approach to error handling. I am using c++.

Cheers,
Joe
InexorableTash · 17 Sep 2004, 05:05 UTC
Your highest priority in writing an application should be to avoid causing data corruption or rendering the system unusable (e.g. going into an infinite loop or deadlocking). In a game - which is typically fullscreen, trapping all button and stylus events, and generating sound - this is easy to fall into. (I did this to myself in my hobby game the other day; I infinite looped in a a mouse handler while in fullscreen mode on the desktop. Oops!)

In many ways, simply calling TerminateProcess() and exiting immediately is preferrable.

Exceptions (which someone is sure to mention) are a very tempting way to avoid having your application exit. But think about an exception occuring deep within your app, then half the stack unwinding to the exception handler, then the app proceeding on as if nothing happened - what state is the data in? Are you trashing your user's files? Is running the app with memory in an unpredictable state really desirable?

(That said, I'm a big fan of coding such that exceptions could occur at any time. Try to avoid writing any block of code where coherence of the app depends on execution making it successfully through. For example, never just write "foo* pMem = new foo();", since an exception in the next line of code would cause an memory leak. Instead, assign the pointer into an object on the stack that will automatically delete the memory when the object goes out of scope. Ditto for any resources, locks, etc. But I digress.)

You should also consider what classes of errors your app will have:

- Is it an assertion failure, that indicates that another part of your app has failed or is misbehaving? In that case you are screwed; your assumptions about how the rest of your own code can't be trusted, so exiting as quickly as possible (possibly popping a MessageBox first) is probably the best.

- Is some external API returning an error code? In that case, is it something you can anticipate or code around?

- Is it a system error (e.g. file not found)? If so, what are you doing to handle it that helps the user? (e.g. can they recover from it?)

- On the extreme side of things, some apps offer auto recovery. Some spew out backup state files on a regular basis so they can revert to them in case of crash. Others go to additional extremes: if the app crashes it will bail out of the main code and use a stub which grovels through memory residue to try to resurrect the user's latest edits. (My guess is that until you have half a billion users the incremental gains of that level of work are not worth it.)

...

Also, consider: what will the user do with your error message? Is it actionable, that is, are they supposed to do something other than curse? Can they fix the problem? Can they provide some data to you which would help you fix the problem? In some ways, the old Amiga "Guru meditation #12345" errors aren't such a bad idea.

...

Those are just some idle thoughts to share while I'm watching TV and waiting for my wife to finish making a web page she wants me to post.
Concentrix takes a tiered approach to error handling.

During development, a lot of error info is written to a log file for later analysis. This helps because it allows errors to be quickly isolated during development, and to ensure that everything is working as it should be (sort of like unit testing in that respect).

The program itself it partitioned into areas so error checking can be performed at the lowest level. For example, if a file is created, everything about the file is checked at that point. Then the program might pass that file to a bunch of functions that don't check that the file handle is good. It is assumed to be good because it already is checked, this is a reasonable assumption. Error codes from function calls are still checked.

(You can think of this as establishing an invariant, then preserving an invariant. If the data was good when created, and each operation on the data keeps the data good, then the data is always good, by induction.)

If an error is detected, functions checking return codes basically unwrap themselves passing the error up. Eventually the program displays an "orange screen of death" to the user describing the error and an error code, so I can know what happened and how to fix it. This happens, for example, if the program detects that some of its files are missing, or if its timing mechanism is somehow not working.

I left some cases where a severe error just exits the program. These would be few and rare. If I can provide a meaningful error message, I try to do so instead. These cases would mostly occur because of a compiler error or similar.

In practice, there have been no orange screens of death or exits after release. However this kind of disciplined approach to error handling helps a lot during development and testing. The reason there are few errors in Concentrix is because I have treated errors seriously and strived to ensure that they do not occur. If they do occur, it's probably because a critical file was deleted or similar.
Many thanks for your suggestions. Now the trick is going back over the code I have already written and adding in the appropirate error handling :)

Cheers,
Joe
InexorableTash · 18 Sep 2004, 06:00 UTC
A few last tips:

mlepage wrote:
(You can think of this as establishing an invariant, then preserving an invariant. If the data was good when created, and each operation on the data keeps the data good, then the data is always good, by induction.)


This is great practice. All code should be inspected and reviewed with this sort of thing in mind. During development, it's best to augment this with assertions - code which verifies that things are as expected. Don't trust other people's code, and don't even trust your own! I use an Assert() macro that in DEBUG builds that pops a "Ignore/Debug/Abort" message box. Assert as many conditions as you can - it doesn't hurt the released code, and it helps catch many coding errors before release.

One more tip - if you can get into the habit of using HRESULTs for error codes (like GapiDraw does) then you can pretty much get into the mindless repetition of coding each procedure like this:
Code:

HRESULT Foo()
{
HRESULT hr;

hr = Bar();
Assert( SUCCEEDED(hr) );
if( FAILED(hr) )
return hr;

// rinse and repeat

return S_OK;
}

At development time you'll catch all of the failures immediately and can track down why they occured - is it a coding mistake or a legitimate downstream failure? At runtime your app will fail immediately.