Preferred coding style for error handling?

Jakob Eriksson jakob at vmlinux.org
Wed Aug 27 02:09:00 CDT 2003


Mike Hearn wrote:

>but obviously pretty much every coding guideline ever written says
>"don't use goto!". Given the lack of any exception handling in C, and so
>  
>

I have done a lot of C and oldstyle C++ coding, and I have never felt
the need for a goto.
I find that I can almost always do it by having many returns instead,
all at the top, checking
for failure modes. That way you avoid the nested ifs and you always know
the real businness
is at the bottom of the function.

    DWORD res = 1;
    HKEY key = NULL;

    res = RegOpenKeyEx(hCurrent, subkey, 0, KEY_ALL_ACCESS, &key);
    if (res != ERROR_SUCCESS) {

	return res;
    }

    .... do stuff with key here .....

    RegCloseKey(key);  /* **) */

    return res;


**) In this particular example I assume that if RegOpenKeyEx returns not
ERROR_SUCCESS,
then the key is invalid and need not closing.




When this approach is not working, I usually find that the function I
try to implement is
too big and need to be split up in subfunctions.



regards,
Jakob






More information about the wine-devel mailing list