M
mbrown
Hi.
I am working on some very long routines with constructs like this
throughout:
a = GetAValue();
if (a == b)
{
Message(Some_Err, "a can not be equal to b");
goto end
}
....
end:
cleanup(someStructPointer);
cleanup(anotherStructPointer);
free(something);
....
I want to factor all of this error handling code into simple routines.
The problem of course is the error handling. If I were using c++ I
would use exceptions and try-catch-finally blocks. Unfortunately I do
not have that option. I'le thought of using macros, like:
VALIDATE(errorCondition, err, message) If(errorCondition) /
{ \
Message(err, message); \
goto end; \
}
But this reduces my flexibility in facoting out larger portions of
code. I also like to initialize variables close to where they are
first used rather than at the start of a routine (my compiler lets me
do this), but the goto statements result in warnings about jumping
over variable initializations.
Is there a better cleaner) way of handling error testing and cleanup
code in C?
I have thought of sticking all cleanup code in a single routine, but
in some of the routines I'm using this would mean passing a dozen or
so parameters to the cleanup routine.
Thanks
Matt Brown
I am working on some very long routines with constructs like this
throughout:
a = GetAValue();
if (a == b)
{
Message(Some_Err, "a can not be equal to b");
goto end
}
....
end:
cleanup(someStructPointer);
cleanup(anotherStructPointer);
free(something);
....
I want to factor all of this error handling code into simple routines.
The problem of course is the error handling. If I were using c++ I
would use exceptions and try-catch-finally blocks. Unfortunately I do
not have that option. I'le thought of using macros, like:
VALIDATE(errorCondition, err, message) If(errorCondition) /
{ \
Message(err, message); \
goto end; \
}
But this reduces my flexibility in facoting out larger portions of
code. I also like to initialize variables close to where they are
first used rather than at the start of a routine (my compiler lets me
do this), but the goto statements result in warnings about jumping
over variable initializations.
Is there a better cleaner) way of handling error testing and cleanup
code in C?
I have thought of sticking all cleanup code in a single routine, but
in some of the routines I'm using this would mean passing a dozen or
so parameters to the cleanup routine.
Thanks
Matt Brown