ugly construct

Discussion in 'C Programming' started by mbrown@arete.com, Feb 20, 2008.

  1. Guest

    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
    , Feb 20, 2008
    #1
    1. Advertising

  2. wrote:
    > 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.

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Feb 20, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=

    Datagrid with fewer records than page size is ugly

    =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=, Feb 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    Alvin Bruney [MVP]
    Feb 29, 2004
  2. cr88192
    Replies:
    3
    Views:
    565
    cr88192
    Sep 7, 2005
  3. Dan Cernat
    Replies:
    18
    Views:
    469
    lilburne
    Nov 8, 2003
  4. ugly construct

    , Feb 20, 2008, in forum: C Programming
    Replies:
    0
    Views:
    310
  5. Mukesh
    Replies:
    4
    Views:
    608
    Paul N
    Mar 26, 2010
Loading...

Share This Page