exception handling mechanism

Discussion in 'C Programming' started by Ralph A. Moeritz, Apr 12, 2005.

  1. Hello,

    I'd like to implement an exception handling mechanism
    in my C programs. Up to now I have been using a local
    error handling scheme where errors are handled (when
    appropriate) in the lowest-level functions eg.

    char *
    foo (void)
    {
    int err = 0; /* (error flag: 1=set) */
    char *str = xmalloc (BUF_LEN);

    int ret = do_something (str);
    if (!ret)
    {
    ++err;
    goto clean_up;
    }

    clean_up:
    if (err)
    {
    /* handle error */
    }

    return str;
    }

    void
    bar (void)
    {
    char *s = foo ();
    /* ... */
    }

    What this sheme lacks is a way for the called function to pass error
    information back to the calling function so that it can deal with it
    in appropriate situations. I've heard of people implementing TRY,
    CATCH, THROW using setjmp & longjmp, but I've never seen any code
    for this...

    Regards,
    Ralph
     
    Ralph A. Moeritz, Apr 12, 2005
    #1
    1. Advertising

  2. "Ralph A. Moeritz" wrote:
    >
    > Hello,
    >
    > I'd like to implement an exception handling mechanism
    > in my C programs. Up to now I have been using a local
    > error handling scheme where errors are handled (when
    > appropriate) in the lowest-level functions eg.
    >
    > char *
    > foo (void)
    > {
    > int err = 0; /* (error flag: 1=set) */
    > char *str = xmalloc (BUF_LEN);
    >
    > int ret = do_something (str);
    > if (!ret)
    > {
    > ++err;
    > goto clean_up;
    > }
    >
    > clean_up:
    > if (err)
    > {
    > /* handle error */
    > }
    >
    > return str;
    > }
    >
    > void
    > bar (void)
    > {
    > char *s = foo ();
    > /* ... */
    > }
    >
    > What this sheme lacks is a way for the called function to pass error
    > information back to the calling function so that it can deal with it
    > in appropriate situations. I've heard of people implementing TRY,
    > CATCH, THROW using setjmp & longjmp, but I've never seen any code
    > for this...
    >
    > Regards,
    > Ralph


    Why make life complicated? There are two extremely simple solutions:
    1)
    int foo( char **result ) {
    if ( result == NULL ) return ERROR_1; /* #define ERROR_1 somewhere */
    *result = malloc(...);
    ...
    if (error) return ERROR_2;
    ...
    return 0;
    }

    void bar(void) {
    char *s;
    int status = foo(&s);
    }

    2)
    char *foo( int *status )

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Common User Interface Services
    M/S 2R-94 (206)544-5225
     
    Fred L. Kleinschmidt, Apr 12, 2005
    #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. Ben Finney

    confusion about Exception Mechanism

    Ben Finney, Jan 15, 2004, in forum: Python
    Replies:
    3
    Views:
    307
    Samuel Walters
    Jan 15, 2004
  2. Replies:
    3
    Views:
    357
    Ben Hutchings
    Dec 13, 2005
  3. Steven Woody

    Event Handling and Signal-Slot Mechanism

    Steven Woody, Jan 19, 2009, in forum: Python
    Replies:
    2
    Views:
    1,067
    Steven Woody
    Jan 20, 2009
  4. Peng Yu
    Replies:
    95
    Views:
    1,517
    Roel Schroeven
    Jan 7, 2010
  5. Rob Hoelz

    Best error handling mechanism?

    Rob Hoelz, Jun 5, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    96
    Lars Haugseth
    Jun 16, 2007
Loading...

Share This Page