centralised error handelling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    centralised error handelling

    Hi, I'm thinking of having a seperate module in my project that deals
    with the exceptions and errors as they occur in a C program. Is this a
    good idea ? I thought of doing this because it is getting really
    repetitive to have the same thing in my code over and voer again eg.
    if malloc fails then:

    int *p;

    p = malloc(sizeof(i nt) * 5);

    if(p == NULL)
    {
    perror("malloc has failed");
    exit(EXIT_FAILU RE);
    }

    This situation may arise in many places in different contexts so what
    I would like to do is identify such cases and have a seperate module
    with a switch case that takes an error flag as an input and makes the
    decision based on it. The error flags would represent the various
    errors/exceptions like malloc failure, failure in opening file, scanf
    failure, floating point exceptions , array out of bounds etc. I would
    also like to print the name of the module where the exception occuerd
    as well as the line number. Is there a function in C which provides
    this facility ?
  • Pietro Cerutti

    #2
    Re: centralised error handelling

    pereges wrote:
    Hi, I'm thinking of having a seperate module in my project that deals
    with the exceptions and errors as they occur in a C program. Is this a
    good idea ? I thought of doing this because it is getting really
    repetitive to have the same thing in my code over and voer again eg.
    if malloc fails then:
    >
    int *p;
    >
    p = malloc(sizeof(i nt) * 5);
    >
    if(p == NULL)
    {
    perror("malloc has failed");
    exit(EXIT_FAILU RE);
    }
    >
    This situation may arise in many places in different contexts so what
    I would like to do is identify such cases and have a seperate module
    with a switch case that takes an error flag as an input and makes the
    decision based on it. The error flags would represent the various
    errors/exceptions like malloc failure, failure in opening file, scanf
    failure, floating point exceptions , array out of bounds etc.
    A good example of this are, IMHO, Marc J. Rochkind's EC_* macros
    described in the "Advanced UNIX Programming" book.

    Have a look at http://basepath.com/aup, especially the examples' source
    code for the first chapter (look for ec.[h|c]).
    I would
    also like to print the name of the module where the exception occuerd
    as well as the line number. Is there a function in C which provides
    this facility ?
    Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
    which expand to the filename and line number where they appear.

    --
    Pietro Cerutti

    Comment

    • Lew Pitcher

      #3
      Re: centralised error handelling

      In comp.lang.c, pereges wrote:
      Hi, I'm thinking of having a seperate module in my project that deals
      with the exceptions and errors as they occur in a C program. Is this a
      good idea ?
      IMHO, for a small number of common exceptions and/or errors, this is a good
      idea, but it breaks down when you want to generalize it, or later add
      behaviour changes.

      [snip]
      This situation may arise in many places in different contexts so what
      I would like to do is identify such cases and have a seperate module
      with a switch case that takes an error flag as an input and makes the
      decision based on it.
      For a limited number of errors or exception conditions, this would work.
      But, it isn't scalable, and your initial design/implementation may not
      properly handle exceptional processing.

      For example, your initial design may simply report the error and call
      exit(). Subsequently, you may find reason to report the error and return
      back to the caller, or report the error and request a user decision, or
      silently handle the error and return. Each one of these changes would
      necessitate corresponding behavioural changes in your common handler, along
      with a (possibly) ever-growing list of function arguments (function,
      message, output-log-file FILE, user-input-action-message,
      user-input-options, user-input-file FILE, user-input-via-GUI flag,
      exit/return code, abnormal-termination-with-core-dump flag, ...).

      You need to be careful about how you design the initial function, and under
      what circumstances it will be used, otherwise accomodating your "exception
      handler" function may consume more programming resources than the rest of
      the program.

      [snip]
      Is there a function in C which provides this facility ?
      Not in ISO standard C, no.

      --
      Lew Pitcher

      Master Codewright & JOAT-in-training | Registered Linux User #112576
      http://pitcher.digitalfreehold.ca/ | GPG public key available by request
      ---------- Slackware - Because I know what I'm doing. ------


      Comment

      • Eric Sosman

        #4
        Re: centralised error handelling

        Pietro Cerutti wrote:
        pereges wrote:
        >[...]
        >I would
        >also like to print the name of the module where the exception occuerd
        >as well as the line number. Is there a function in C which provides
        >this facility ?
        >
        Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
        which expand to the filename and line number where they appear.
        __FILE__ and __LINE__ are part of Standard C. In C99
        there is also __func__ for the name of the enclosing function
        (it's a plain identifier, though, not a macro).

        --
        Eric.Sosman@sun .com

        Comment

        • Antoninus Twink

          #5
          Re: centralised error handelling

          On 19 May 2008 at 18:01, Pietro Cerutti wrote:
          Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
          which expand to the filename and line number where they appear.
          You could also just dump core, which would give you access to a full
          backtrace - very useful for debugging.

          Comment

          • Pietro Cerutti

            #6
            Re: centralised error handelling

            Eric Sosman wrote:
            Pietro Cerutti wrote:
            >pereges wrote:
            >>[...]
            >>I would
            >>also like to print the name of the module where the exception occuerd
            >>as well as the line number. Is there a function in C which provides
            >>this facility ?
            >>
            >Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
            >which expand to the filename and line number where they appear.
            >
            __FILE__ and __LINE__ are part of Standard C.
            Right.


            --
            Pietro Cerutti

            Comment

            • pereges

              #7
              Re: centralised error handelling

              can someone please give a simple example demonstrating _FILE_, _LINE_
              and _func_

              Comment

              • Pietro Cerutti

                #8
                Re: centralised error handelling

                pereges wrote:
                can someone please give a simple example demonstrating _FILE_, _LINE_
                and _func_
                >
                cat filelinefunc.c
                #include <stdio.h>
                int main(void)
                {
                printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__);
                return (0);
                }
                ./filelinefunc
                filelinefunc.c: 4 (main)




                --
                Pietro Cerutti

                Comment

                • pereges

                  #9
                  Re: centralised error handelling

                  On May 19, 11:27 pm, Pietro Cerutti <g...@gahr.chwr ote:
                  pereges wrote:
                  can someone please give a simple example demonstrating _FILE_, _LINE_
                  and _func_
                  >
                  cat filelinefunc.c
                  #include <stdio.h>
                  int main(void)
                  {
                  printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__);
                  return (0);}
                  >
                  ./filelinefunc
                  filelinefunc.c: 4 (main)
                  >
                  why the paranthesis for the last %s format specifier ?

                  Comment

                  • Pietro Cerutti

                    #10
                    Re: centralised error handelling

                    pereges wrote:
                    On May 19, 11:27 pm, Pietro Cerutti <g...@gahr.chwr ote:
                    >pereges wrote:
                    >>can someone please give a simple example demonstrating _FILE_, _LINE_
                    >>and _func_
                    > cat filelinefunc.c
                    >#include <stdio.h>
                    >int main(void)
                    >{
                    > printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__);
                    > return (0);}
                    >>
                    > ./filelinefunc
                    >filelinefunc.c :4 (main)
                    >>
                    >
                    why the paranthesis for the last %s format specifier ?
                    Because I wanted the function name to appear between parenthesis... it's
                    part of the format string.


                    --
                    Pietro Cerutti

                    Comment

                    • Flash Gordon

                      #11
                      Re: centralised error handelling

                      Antoninus Twink wrote, On 19/05/08 19:14:
                      On 19 May 2008 at 18:01, Pietro Cerutti wrote:
                      >Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
                      >which expand to the filename and line number where they appear.
                      >
                      You could also just dump core, which would give you access to a full
                      backtrace - very useful for debugging.
                      Unless the system does not produce a core dump or equivalent. Systems
                      which do not produce core dumps are quite common.
                      --
                      Flash Gordon

                      Comment

                      • CBFalconer

                        #12
                        Re: centralised error handelling

                        Pietro Cerutti wrote:
                        pereges wrote:
                        >
                        .... snip ...
                        >
                        >I would also like to print the name of the module where the
                        >exception occuerd as well as the line number. Is there a
                        >function in C which provides this facility ?
                        >
                        Standard C doesn't, but GCC provides the __FILE__ and __LINE__
                        macros, which expand to the filename and line number where
                        they appear.
                        Those macros are provided by C99. Also C89, C90, and C95. I.E.
                        Standard C does provide them.

                        --
                        [mail]: Chuck F (cbfalconer at maineline dot net)
                        [page]: <http://cbfalconer.home .att.net>
                        Try the download section.


                        ** Posted from http://www.teranews.com **

                        Comment

                        • Pietro Cerutti

                          #13
                          Re: centralised error handelling

                          CBFalconer wrote:
                          Pietro Cerutti wrote:
                          >pereges wrote:
                          >>
                          ... snip ...
                          >>I would also like to print the name of the module where the
                          >>exception occuerd as well as the line number. Is there a
                          >>function in C which provides this facility ?
                          >Standard C doesn't, but GCC provides the __FILE__ and __LINE__
                          >macros, which expand to the filename and line number where
                          >they appear.
                          >
                          Those macros are provided by C99. Also C89, C90, and C95. I.E.
                          Standard C does provide them.
                          Yes, see my answer to Eric.

                          --
                          Pietro Cerutti

                          Comment

                          • Bill Reid

                            #14
                            Re: centralised error handelling


                            Lew Pitcher <lpitcher@teksa vvy.comwrote in message
                            news:3e26f$4831 c116$4c0a8b34$3 2733@TEKSAVVY.C OM-Free...
                            In comp.lang.c, pereges wrote:
                            >
                            Hi, I'm thinking of having a seperate module in my project that deals
                            with the exceptions and errors as they occur in a C program. Is this a
                            good idea ?
                            >
                            IMHO, for a small number of common exceptions and/or errors, this is a
                            good
                            idea, but it breaks down when you want to generalize it, or later add
                            behaviour changes.
                            Hmmmm, well, not necessarily...I think you have to come up with
                            the right generalized scheme. At least I seem to have gained some
                            good practical coding speed using such a scheme.
                            [snip]
                            This situation may arise in many places in different contexts so what
                            I would like to do is identify such cases and have a seperate module
                            with a switch case that takes an error flag as an input and makes the
                            decision based on it.
                            >
                            For a limited number of errors or exception conditions, this would work.
                            But, it isn't scalable, and your initial design/implementation may not
                            properly handle exceptional processing.
                            I think the trick, as always with low-level support library functions, is
                            to abstract what you are doing over and over and over again in the
                            application,
                            and generalize it to the extent possible.

                            For errors, you have a severity level of error (warning, caution, etc.), a
                            logging/reporting "requiremen t", possible prompted user actions that may
                            still fail to fix the problem, and the application action that is taken if
                            the
                            problem cannot be solved by user action. Most, but not all, of these
                            can and should be handled generically at a lower-level...
                            For example, your initial design may simply report the error and call
                            exit().
                            Both of these can be handled in a low-level generic library function...
                            Subsequently, you may find reason to report the error and return
                            back to the caller,
                            IBID. As a matter of fact, the way I write my particular applications,
                            you are generally kicked out of the operation gracefully and returned
                            to a "menu" state to select another operation, with a timed "dialog"
                            asking the user if they want to continue with the application at all...
                            or report the error and request a user decision,
                            IBID.
                            or
                            silently handle the error and return.
                            IBID, though you'd still probably want the temporary failure to
                            be logged...
                            Each one of these changes would
                            necessitate corresponding behavioural changes in your common handler,
                            along
                            with a (possibly) ever-growing list of function arguments (function,
                            message, output-log-file FILE, user-input-action-message,
                            user-input-options, user-input-file FILE, user-input-via-GUI flag,
                            exit/return code, abnormal-termination-with-core-dump flag, ...).
                            Well, I didn't say it would be EASY, just a little easier (maybe!)
                            than re-inventing all those wheels for EVERY possible error...and
                            you've really got to pick your specific and generic handling
                            carefully, and handle each at the appropriate level...
                            You need to be careful about how you design the initial function, and
                            under
                            what circumstances it will be used, otherwise accomodating your "exception
                            handler" function may consume more programming resources than the rest of
                            the program.
                            Well, if you're talking about exceptions AND errors, maybe (I'm
                            starting to be reminded of "try-catch" in other languages)...
                            Is there a function in C which provides this facility ?
                            >
                            Not in ISO standard C, no.
                            What about "errno.h" and stuff like that?

                            ---
                            William Ernest Reid



                            Comment

                            • Peter Nilsson

                              #15
                              Re: centralised error handelling

                              Pietro Cerutti wrote:
                              ...
                              A good example of this are, IMHO, Marc J. Rochkind's EC_*
                              macros described in the "Advanced UNIX Programming" book.
                              Poor choice of namespace. Identifiers begining with E and
                              a capital letter are reserved whenever <errno.his included.

                              --
                              Peter

                              Comment

                              Working...