PocketMatrix forums

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

Error handling?

How are people doing error handling in their Pocket PC programs?

There's the traditional style of having each function return an error code, and having each caller check that code for success or failure. I don't mind that, but I do find it can be tedious and make the code a little ugly.

An augment to that style is to have a global place to set additional error info, and check that for more info if an error code was returned. This is like errno in standard C.

Then, there's the non-local jumping and automatic object destruction of standard C++ exceptions. I find exceptions useful, but my understanding is that they are not supported in eVC3, only in eVC4. Are people using exceptions in eVC4?

Microsoft has its own exception-like mechanism in C and C++, which I assume is supported in eVC3. Are people using this? What's it like?

You can find similar exception-like libraries on the web, or roll your own using setjmp/longjmp. Compared to Microsoft's, they'll have pros and cons. What are your experiences?

Another option is to use setjmp/longjmp in a less structured, more adhoc way. I imagine this could get messy, I've never done it.

Another option, going further down that road, is to be like Quake 2 and basically have one error function which does a longjmp to one error handling place. I think this is OK, but it doesn't really leave much options for handling the error, or recovering from it.

Is anyone using signals to handle errors?

So, what are people doing to handle errors in their Pocket PC programs?
mlepage · 23 Aug 2003, 16:51 UTC — My Experience
I'll chime in with my views. I'm using eVC3 which is the source of some of my limits.

I would prefer to use standard C++ exceptions, as they work relatively nicely and handle auto object destruction (so resources aren't leaked in the event of an error).

Instead, I spent a bit of time yesterday implementing my own exception-like macros using setjmp/longjmp. They work OK, but of course don't do the auto object destruction.

My nascent app hasn't yet stabilized into one error handling scheme, though that's a new priority for me now. Indeed, I might use two schemes, including traditional error return codes, and exception-like mechanisms.
mlepage · 23 Aug 2003, 16:54 UTC — eVC4 exceptions?
I'd be interested to know what is up-and-coming for standard C++ exception support for Pocket PC development. Last time I checked, I saw eVC4 supported it, but couldn't target Pocket PC 2002, if I understand correctly.

Has this changed now? What about .NET?

Basically, I'm still using eVC3 and unaware of what the current options are for tools, and what each one supports and doesn't support.

Of all the things eVC3 is missing, exceptions is probably the thing I miss most, followed by iostreams.
I have myself tried various solutions since 1998 when I first had to develop some Pocket PC (or rather Palm-Sized PC) software.

Microsoft exceptions aka SEH (Structured Exception Handling) are not any more useful for what you describe than setjmp/longjmp. Objects on the stack will not be cleaned up in either case.

However, stack clean-up can be added by having all classes that can potentially be constructed on the stack inherit a certain base class. This method was used in http://www.movsoftware.com/execpt.htm. I have used this package sometimes but I find it to be a bit too intrusive to have to add various base classes etc especially if you want to maintain the same code for multiple platforms.

For Windows CE C++ exception handling is not strictly a compiler issue so although the evc4 compilers support C++ exception handling, that cannot be used on Pocket PC 2002 which is based on WCE 3.0. However, for Pocket PC 2003 which is based on WCE 4.20 you do have C++ exceptions, RTTI etc.

So, basically, things will not be great until WCE 3.x support can be dropped entirely and this may be a while into the future because of all non-upgradeable but still fairly modern Pocket PC 2002:s.

/Orpe
Orpe, I understand what you're saying. I have an iPAQ 3970, which has not been upgraded to 2003 although I understand it could be.

So if there are no current plans to make eVC3 support exceptions, or eVC4 target WCE3, (and I assume neither are currently planned), then I will continue to NOT use standard C++ exceptions as a solution.

Instead, I will continue to use other methods (like my exception-like macros and traditional return codes) so as to support these widely available 2002 devices.
mlepage · 24 Aug 2003, 04:20 UTC — Implementing Exceptions
I described my exception-like mechanism in other topic "Implementing Exceptions Using setjmp/longjmp"

http://www.pocketmatrix.com/forums/viewtopic.php?t=12430
Maybe it's just me, but I don't have problems writing code to check return values from functions that might have failed. The only thing that I rely on for exception handling is for data misalignment, access violations, and floating exceptions. These are truly exceptional cases so I'll wrap them in a Win32 SEH block. Running out of memory isn't an exceptional case on a Pocket PC. Neither is not being able to open a file. Those sort of things are easily determined by checking the return value. The other thing that probably saves me is that I don't create C++ objects on the stack (or if I do, they rarely do anything in the dtor).

In the end is my code "uglier" with all of the checking of return values? Maybe, but the person who pays for the software doesn't care about how ugly it is. In the odd chance that someone else would have to maintain the code other than me, at least they would see my code testing the return value when the actual call is being made rather than 5 calling functions up the stack (hopefully) with an exception handler. One could argue that might actually make the code easier to understand.

In the end if I had the choice I'd pick well-commented code over pretty code any day of the week.
For me, C++ exceptions are sort of like the scroll wheel on a mouse. Once you've gotten used to them you almost cannot accept being without them.

There's nothing that is impossible to accomplish without them, but certain things take a lot less work with them.

Inspecting return values and handling any errors in most cases cause very little problems. However, in some cases the point where you get the error is not the correct place for handling it. It takes some work to solve this in a nice way when exceptions are not available. The various error codes need to be communicated upwards and it is easy to make mistakes.
This solution is not any easier to understand than the exceptions solution and with exceptions you get it for free.

I also prefer well commented over pretty code, but I prefer well commented pretty code over that. With exceptions, you generally wont have to write as much code which further reduces the number of errors you risk making.

In the end, it's a matter of taste. But not having exceptions often creates problem when taking code from other platforms.