Exception trapping in C?

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

    #1

    Exception trapping in C?





    One of the features from other languages that I miss most in C is
    trappable exceptions. More specifically, I think it's great to be
    able to demarcate a whole block of code where several exceptions
    can happen at various points, so that any one of these exceptions
    can be trapped and handled by the exception-handling code collected
    in one place after the block. This also means that the exception
    generating code can be much cleaner, since all it has to do is
    raise the exception locally and bomb. In contrast, in traditional
    C programming, many, if not all, function calls have to be surrounded
    by error-checking and error-handling code. The error checking can
    easily overwhelm the code.

    Is there any way to implement something like C++'s try/catch
    mechanism for exception trapping in C?

    Thanks!

    Karl
    --
    Sent from a spam-bucket account; I check it once in a blue moon. If
    you still want to e-mail me, cut out the extension from my address,
    and make the obvious substitutions on what's left.
  • Eric Sosman

    #2
    Re: Exception trapping in C?

    KKramsch wrote:[color=blue]
    > One of the features from other languages that I miss most in C is
    > trappable exceptions. More specifically, I think it's great to be
    > able to demarcate a whole block of code where several exceptions
    > can happen at various points, so that any one of these exceptions
    > can be trapped and handled by the exception-handling code collected
    > in one place after the block. This also means that the exception
    > generating code can be much cleaner, since all it has to do is
    > raise the exception locally and bomb. In contrast, in traditional
    > C programming, many, if not all, function calls have to be surrounded
    > by error-checking and error-handling code. The error checking can
    > easily overwhelm the code.
    >
    > Is there any way to implement something like C++'s try/catch
    > mechanism for exception trapping in C?[/color]

    Many people have used setjmp() and longjmp() to build
    exception facilities of varying degrees of sophistication.
    The lack of "linguistic " support means that setjmp() and
    longjmp() must be used with great care, and that the compiler
    is usually unable to warn you about misuse. There are also
    some nasty issues about the values of non-volatile variables
    after longjmp().

    A more modest function-local error-handling scheme can
    be built with the `goto' statement. Each fallible operation
    must still be tested for failure, but the action upon failure
    can be to `goto' a chunk of all-purpose recovery code. This
    code will clean up by freeing dynamic memory, closing opened
    files, and so on, and typically returns a failure code as the
    function value. Unlike setjmp()/longjmp() schemes, this sort
    of thing is "linguistically " supported and the compiler can
    detect some errors (e.g., trying to `goto' a label that's not
    in the same function).

    --
    Eric.Sosman@sun .com

    Comment

    • jacob navia

      #3
      Re: Exception trapping in C?

      KKramsch wrote:[color=blue]
      > One of the features from other languages that I miss most in C is
      > trappable exceptions. More specifically, I think it's great to be
      > able to demarcate a whole block of code where several exceptions
      > can happen at various points, so that any one of these exceptions
      > can be trapped and handled by the exception-handling code collected
      > in one place after the block. This also means that the exception
      > generating code can be much cleaner, since all it has to do is
      > raise the exception locally and bomb. In contrast, in traditional
      > C programming, many, if not all, function calls have to be surrounded
      > by error-checking and error-handling code. The error checking can
      > easily overwhelm the code.
      >
      > Is there any way to implement something like C++'s try/catch
      > mechanism for exception trapping in C?
      >
      > Thanks!
      >
      > Karl[/color]

      The lcc-win32 compiler implements the exception handling proposed
      by Microsoft for the windows system, and implemented in most windows
      compilers
      __try {
      // protected block
      }
      __except(EXCEPT _EXECUTE_HANDLE R) {
      exception handling block
      }

      In future versions this will be extended to try/catch.

      The problems with setjmp/longjmp (that also can be used to build
      exception handling mechanisms) is that you have to write code to
      handle all exceptions one by one.

      It would be nice if the C language would standardize the use
      of catch/throw, but surely this is too much asking, hence this
      non portable solution.

      For a discussion about this see
      ftp://ftp.cs.virginia.edu:/pub/lcc-win32/tutorial.pdf

      See there 1.34.5 Structured exception handling.

      Comment

      • Dave Vandervies

        #4
        Re: Exception trapping in C?

        In article <41b0b94d$0$905 8$8fcfb975@news .wanadoo.fr>,
        jacob navia <jacob@jacob.re mcomp.fr> wrote:[color=blue]
        >KKramsch wrote:[/color]
        [color=blue][color=green]
        >> Is there any way to implement something like C++'s try/catch
        >> mechanism for exception trapping in C?[/color][/color]
        [color=blue]
        >The lcc-win32 compiler implements the exception handling proposed
        >by Microsoft for the windows system,[/color]

        I'm sure if the OP wanted an implementation-specific answer, he'd've
        posted in an implementation-specific group.

        Please restrict your comments in comp.lang.c to the C language itself;
        there are plenty of other, more appropriate, places to discuss proprietary
        extensions.


        dave

        --
        Dave Vandervies dj3vande@csclub .uwaterloo.ca
        I later told my sister (23 years old) that I had got a personal reply from
        Dennis Ritchie. She said she didn't believe it. What is funny is that my sister
        ... probably doesn't even know who Dennis Ritchie is. --Joona Palaste in CLC

        Comment

        • Merrill & Michele

          #5
          Re: Exception trapping in C?


          "Eric Sosman" <eric.sosman@su n.com> wrote in message
          news:coqb7u$8r9 $1@news1brm.Cen tral.Sun.COM...[color=blue]
          > KKramsch wrote:[color=green]
          > > One of the features from other languages that I miss most in C is
          > > trappable exceptions. More specifically, I think it's great to be
          > > able to demarcate a whole block of code where several exceptions
          > > can happen at various points, so that any one of these exceptions
          > > can be trapped and handled by the exception-handling code collected
          > > in one place after the block. This also means that the exception
          > > generating code can be much cleaner, since all it has to do is
          > > raise the exception locally and bomb. In contrast, in traditional
          > > C programming, many, if not all, function calls have to be surrounded
          > > by error-checking and error-handling code. The error checking can
          > > easily overwhelm the code.
          > >
          > > Is there any way to implement something like C++'s try/catch
          > > mechanism for exception trapping in C?[/color]
          >
          > Many people have used setjmp() and longjmp() to build
          > exception facilities of varying degrees of sophistication.
          > The lack of "linguistic " support means that setjmp() and
          > longjmp() must be used with great care, and that the compiler
          > is usually unable to warn you about misuse. There are also
          > some nasty issues about the values of non-volatile variables
          > after longjmp().
          >
          > A more modest function-local error-handling scheme can
          > be built with the `goto' statement. Each fallible operation
          > must still be tested for failure, but the action upon failure
          > can be to `goto' a chunk of all-purpose recovery code. This
          > code will clean up by freeing dynamic memory, closing opened
          > files, and so on, and typically returns a failure code as the
          > function value. Unlike setjmp()/longjmp() schemes, this sort
          > of thing is "linguistically " supported and the compiler can
          > detect some errors (e.g., trying to `goto' a label that's not
          > in the same function).[/color]

          Douglas Gwyn in news:2YudnTjaca 2vkS3cRVn-1w@comcast.com
          quote
          The need for exception handling is amply demonstrated
          by the number of independent kludge implementations
          of it, based on setjmp/longjmp since that is the only
          fe[a]sible way to do anything of the kind in Standard C,
          combined with the evident deficiencies of such kludges.

          One hindering factor would be the matter of C++
          compatibility, which is hard if not impossible to
          achieve without extending C in ways we have in the
          past rejected.
          end quote

          As for me, I wonder why C is lightning-fast while a change to C would be
          only be described as glacial if the passage of time could be reckoned in the
          language at all. MPJ


          Comment

          • KKramsch

            #6
            Re: Exception trapping in C?

            In <5sednUDTu9yXXC 3cRVn-oQ@comcast.com> "Merrill & Michele" <beckjensen@com cast.net> writes:[color=blue]
            >Douglas Gwyn in news:2YudnTjaca 2vkS3cRVn-1w@comcast.com
            >quote
            >The need for exception handling is amply demonstrated
            >by the number of independent kludge implementations
            >of it, based on setjmp/longjmp since that is the only
            >fe[a]sible way to do anything of the kind in Standard C,
            >combined with the evident deficiencies of such kludges.[/color]

            Can anyone point me to any one such (open/vendor-independent)
            implementations ? I'm thinking that such a kluge, imperfect as it
            may be, could be useful if used carefully during development and
            testing and then disabled in the production code.

            Karl

            --
            Sent from a spam-bucket account; I check it once in a blue moon. If
            you still want to e-mail me, cut out the extension from my address,
            and make the obvious substitutions on what's left.

            Comment

            • Merrill & Michele

              #7
              Re: Exception trapping in C?

              [color=blue]
              > "KKramsch"[color=green]
              > > "Merrill & Michele" <beckjensen@com cast.net> writes:
              > >Douglas Gwyn in news:2YudnTjaca 2vkS3cRVn-1w@comcast.com
              > >quote
              > >The need for exception handling is amply demonstrated
              > >by the number of independent kludge implementations
              > >of it, based on setjmp/longjmp since that is the only
              > >fe[a]sible way to do anything of the kind in Standard C,
              > >combined with the evident deficiencies of such kludges.[/color]
              >
              > Can anyone point me to any one such (open/vendor-independent)
              > implementations ? I'm thinking that such a kluge, imperfect as it
              > may be, could be useful if used carefully during development and
              > testing and then disabled in the production code.[/color]

              When you get some code to look at, send it along. Keep in mind C89:

              4 An invocation of the setjmp macro shall appear only in one of
              the following contexts:

              - the entire controlling expression of a selection or iteration
              statement;

              - one operand of a relational or equality operator with the
              other operand an integer constant expression, with the
              resulting expression being the entire controlling expression
              of a selection or iteration statement;

              - the operand of a unary ! operator with the resulting expression
              being the entire controlling expression of a selection or
              iteration statement; or

              - the entire expression of an expression statement (possibly
              cast to void).

              The OPer of this I call Vlad the Impaler. He has extraordinary respect for
              such matters, and I would not cross him. MPJ



              Comment

              • Dan Pop

                #8
                Re: Exception trapping in C?

                In <coq9mq$cmp$1@r eader1.panix.co m> KKramsch <karlUNDERSCORE kramsch@yahooPE RIODcom.invalid > writes:
                [color=blue]
                >Is there any way to implement something like C++'s try/catch
                >mechanism for exception trapping in C?[/color]

                Why bother? If you need C++, you know where to find it.

                BTW, C++ exceptions are one of the major headaches of the C++
                implementors.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de
                Currently looking for a job in the European Union

                Comment

                • Keith Thompson

                  #9
                  Re: Exception trapping in C?

                  jacob navia <jacob@jacob.re mcomp.fr> writes:[color=blue]
                  > KKramsch wrote:[/color]
                  [...][color=blue][color=green]
                  >> Is there any way to implement something like C++'s try/catch
                  >> mechanism for exception trapping in C?
                  >> Thanks!
                  >> Karl[/color]
                  >
                  > The lcc-win32 compiler implements the exception handling proposed
                  > by Microsoft for the windows system, and implemented in most windows
                  > compilers
                  > __try {
                  > // protected block
                  > }
                  > __except(EXCEPT _EXECUTE_HANDLE R) {
                  > exception handling block
                  > }[/color]

                  That's nice, but the previous poster asked about implementing it *in C*.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • jacob navia

                    #10
                    Re: Exception trapping in C?

                    Dave Vandervies wrote:[color=blue]
                    > In article <41b0b94d$0$905 8$8fcfb975@news .wanadoo.fr>,
                    > jacob navia <jacob@jacob.re mcomp.fr> wrote:
                    >[color=green]
                    >>KKramsch wrote:[/color]
                    >
                    >[color=green][color=darkred]
                    >>>Is there any way to implement something like C++'s try/catch
                    >>>mechanism for exception trapping in C?[/color][/color]
                    >
                    >[color=green]
                    >>The lcc-win32 compiler implements the exception handling proposed
                    >>by Microsoft for the windows system,[/color]
                    >
                    >
                    > I'm sure if the OP wanted an implementation-specific answer, he'd've
                    > posted in an implementation-specific group.
                    >[/color]

                    If you had cared to *read* what the original poster wrote you would
                    have seen:
                    [color=blue]
                    > One of the features from other languages that I miss most in C is
                    > trappable exceptions.[/color]

                    That was the first sentence of his post.
                    [color=blue]
                    > Please restrict your comments in comp.lang.c to the C language itself;
                    > there are plenty of other, more appropriate, places to discuss proprietary
                    > extensions.[/color]

                    I do not see any propietary extensions in a construct that is widely
                    used by all windows compilers. I am not claiming any copyright in
                    that either.

                    jacob

                    Comment

                    • William Ahern

                      #11
                      Re: Exception trapping in C?

                      KKramsch <karlUNDERSCORE kramsch@yahoope riodcom.invalid > wrote:[color=blue]
                      > In <5sednUDTu9yXXC 3cRVn-oQ@comcast.com> "Merrill & Michele" <beckjensen@com cast.net> writes:[color=green]
                      > >Douglas Gwyn in news:2YudnTjaca 2vkS3cRVn-1w@comcast.com
                      > >quote
                      > >The need for exception handling is amply demonstrated
                      > >by the number of independent kludge implementations
                      > >of it, based on setjmp/longjmp since that is the only
                      > >fe[a]sible way to do anything of the kind in Standard C,
                      > >combined with the evident deficiencies of such kludges.[/color][/color]
                      [color=blue]
                      > Can anyone point me to any one such (open/vendor-independent)
                      > implementations ? I'm thinking that such a kluge, imperfect as it
                      > may be, could be useful if used carefully during development and
                      > testing and then disabled in the production code.[/color]

                      You can download a nice implementation from

                      Download cexcept: exception handling in C for free. A header file (cexcept.h) that provides Try/Throw/Catch macros similar to those available in C++ for error handling.


                      The "official" site is

                      Source code and documentation for cexcept, a set of macros providing try/catch/throw exception handling in C.


                      Alternate implementations are also listed.


                      Comment

                      • Flash Gordon

                        #12
                        Re: Exception trapping in C?

                        On Fri, 03 Dec 2004 22:57:31 +0100
                        jacob navia <jacob@jacob.re mcomp.fr> wrote:
                        [color=blue]
                        > Dave Vandervies wrote:[color=green]
                        > > In article <41b0b94d$0$905 8$8fcfb975@news .wanadoo.fr>,
                        > > jacob navia <jacob@jacob.re mcomp.fr> wrote:
                        > >[color=darkred]
                        > >>KKramsch wrote:[/color]
                        > >
                        > >[color=darkred]
                        > >>>Is there any way to implement something like C++'s try/catch
                        > >>>mechanism for exception trapping in C?[/color]
                        > >
                        > >[color=darkred]
                        > >>The lcc-win32 compiler implements the exception handling proposed
                        > >>by Microsoft for the windows system,[/color]
                        > >
                        > >
                        > > I'm sure if the OP wanted an implementation-specific answer, he'd've
                        > > posted in an implementation-specific group.
                        > >[/color]
                        >
                        > If you had cared to *read* what the original poster wrote you would
                        > have seen:[/color]

                        I'm sure Dave did read the post, since his answer, unlike yours, shows
                        an understanding of what was being asked for.
                        [color=blue][color=green]
                        > > One of the features from other languages that I miss most in C is
                        > > trappable exceptions.[/color]
                        >
                        > That was the first sentence of his post.[/color]

                        Yes, which provided context but did not ask anything.
                        [color=blue][color=green]
                        > > Please restrict your comments in comp.lang.c to the C language
                        > > itself; there are plenty of other, more appropriate, places to
                        > > discuss proprietary extensions.[/color]
                        >
                        > I do not see any propietary extensions in a construct that is widely
                        > used by all windows compilers.[/color]

                        I'm not aware of it being available in gcc, a compiler I have been using
                        for a few years to produce *native* windows executables.
                        [color=blue]
                        > I am not claiming any copyright in
                        > that either.[/color]

                        No, you are once again suggesting using extensions and trying to promote
                        your compiler when someone asks for a *C* solution.
                        --
                        Flash Gordon
                        Living in interesting times.
                        Although my email address says spam, it is real and I read it.

                        Comment

                        • CBFalconer

                          #13
                          Re: Exception trapping in C?

                          jacob navia wrote:[color=blue]
                          > KKramsch wrote:
                          >[color=green]
                          > > One of the features from other languages that I miss most in C is
                          > > trappable exceptions. More specifically, I think it's great to be
                          > > able to demarcate a whole block of code where several exceptions
                          > > can happen at various points, so that any one of these exceptions
                          > > can be trapped and handled by the exception-handling code collected
                          > > in one place after the block. This also means that the exception
                          > > generating code can be much cleaner, since all it has to do is
                          > > raise the exception locally and bomb. In contrast, in traditional
                          > > C programming, many, if not all, function calls have to be surrounded
                          > > by error-checking and error-handling code. The error checking can
                          > > easily overwhelm the code.
                          > >
                          > > Is there any way to implement something like C++'s try/catch
                          > > mechanism for exception trapping in C?[/color]
                          >
                          > The lcc-win32 compiler implements the exception handling proposed
                          > by Microsoft for the windows system, and implemented in most windows
                          > compilers[/color]
                          .... snip ...

                          A better answer, which would not generate the usual chorus of
                          off-topic protests about this system, would have been:

                          "I supply a non-standard C system restricted to windows which
                          includes exception handling. You can find out about it on the
                          comp.compilers. lcc newsgroup. Cross-posted there and follow-ups
                          set, since it is OT on c.l.c."

                          This has been so cross-posted with follow-ups set.

                          --
                          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                          Available for consulting/temporary embedded and systems.
                          <http://cbfalconer.home .att.net> USE worldnet address!


                          Comment

                          • KKramsch

                            #14
                            Re: Exception trapping in C?

                            In <3d6782-9er.ln1@wilbur. 25thandClement. com> William Ahern <william@wilbur .25thandClement .com> writes:
                            [color=blue]
                            >KKramsch <karlUNDERSCORE kramsch@yahoope riodcom.invalid > wrote:[color=green]
                            >> In <5sednUDTu9yXXC 3cRVn-oQ@comcast.com> "Merrill & Michele" <beckjensen@com cast.net> writes:[color=darkred]
                            >> >Douglas Gwyn in news:2YudnTjaca 2vkS3cRVn-1w@comcast.com
                            >> >quote
                            >> >The need for exception handling is amply demonstrated
                            >> >by the number of independent kludge implementations
                            >> >of it, based on setjmp/longjmp since that is the only
                            >> >fe[a]sible way to do anything of the kind in Standard C,
                            >> >combined with the evident deficiencies of such kludges.[/color][/color][/color]
                            [color=blue][color=green]
                            >> Can anyone point me to any one such (open/vendor-independent)
                            >> implementations ? I'm thinking that such a kluge, imperfect as it
                            >> may be, could be useful if used carefully during development and
                            >> testing and then disabled in the production code.[/color][/color]
                            [color=blue]
                            >You can download a nice implementation from[/color]
                            [color=blue]
                            >http://cexcept.sourceforge.net/[/color]
                            [color=blue]
                            >The "official" site is[/color]
                            [color=blue]
                            >http://www.nicemice.net/cexcept/[/color]
                            [color=blue]
                            >Alternate implementations are also listed.[/color]

                            Great! Thanks!

                            Karl

                            --
                            Sent from a spam-bucket account; I check it once in a blue moon. If
                            you still want to e-mail me, cut out the extension from my address,
                            and make the obvious substitutions on what's left.

                            Comment

                            • Friedrich Dominicus

                              #15
                              Re: Exception trapping in C?

                              >[color=blue]
                              > Great! Thanks![/color]
                              Let me add:
                              http://home.rochester.rr.com/bigbyofrocny/GEF/GEF.html

                              Am however not sure how standard conformant it really is.

                              Regards
                              Friedrich

                              --
                              Please remove just-for-news- to reply via e-mail.

                              Comment

                              Working...