This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Exceptional Methods
  Submitted by

Here's a way to turn access faults that might crash your program into C++ exceptions that you can handle.

It's a rather simple concept, but not many people are using it. You can put one line of code at the beginning of each method of each class, and this will save you from a lot of trouble stepping through your program line by line, trying to find where you have invalid pointers.

In each method, simply check the this pointer. One way to do it is to put a word at the beginning of each method's implementation:


#define CHECKTHIS somewhere in a global .h file to say

if (!this) throw AccessException

When you're ready to compile a release of your project, you can define CHECKTHIS to expand to nothing. This will make your program a little faster. If you are willing to forego the 0.01% speed increase, you can leave it so if a future user of your program ever stumbles upon a NULL pointer, you could treat it gracefully rather than crash out. Especially if you're leaving the poor user in that 640x480 fullscreen mode :)

Of course, this doesn't work for everything. If you're passing pointers to other routines, be sure they're valid.

Greg Magarshak

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.