ugly construct

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
 
M

Mark McIntyre

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
}

Euh - yuck....
...
end:
cleanup(someStructPointer);
cleanup(anotherStructPointer);
free(something);
....

I want to factor all of this error handling code into simple routines.

Myself, I'd step back and consider a larger rewrite. Whoever wrote this
programme in the first place apparently didn't do any planning and
rammed in error handling as an afterthought.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top