Exception trapping in C?

Discussion in 'C Programming' started by KKramsch, Dec 3, 2004.

  1. KKramsch

    KKramsch Guest

    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.
     
    KKramsch, Dec 3, 2004
    #1
    1. Advertising

  2. KKramsch

    Eric Sosman Guest

    KKramsch wrote:
    > 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?


    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, Dec 3, 2004
    #2
    1. Advertising

  3. KKramsch

    jacob navia Guest

    KKramsch wrote:
    > 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


    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_HANDLER) {
    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.
     
    jacob navia, Dec 3, 2004
    #3
  4. In article <41b0b94d$0$9058$>,
    jacob navia <> wrote:
    >KKramsch wrote:


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


    >The lcc-win32 compiler implements the exception handling proposed
    >by Microsoft for the windows system,


    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
    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
     
    Dave Vandervies, Dec 3, 2004
    #4
  5. "Eric Sosman" <> wrote in message
    news:coqb7u$8r9$...
    > KKramsch wrote:
    > > 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?

    >
    > 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).


    Douglas Gwyn in news:
    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
     
    Merrill & Michele, Dec 3, 2004
    #5
  6. KKramsch

    KKramsch Guest

    In <> "Merrill & Michele" <> writes:
    >Douglas Gwyn in news:
    >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.


    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.
     
    KKramsch, Dec 3, 2004
    #6

  7. > "KKramsch"
    > > "Merrill & Michele" <> writes:
    > >Douglas Gwyn in news:
    > >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.

    >
    > 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.


    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
     
    Merrill & Michele, Dec 3, 2004
    #7
  8. KKramsch

    Dan Pop Guest

    In <coq9mq$cmp$> KKramsch <> writes:

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


    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:
    Currently looking for a job in the European Union
     
    Dan Pop, Dec 3, 2004
    #8
  9. jacob navia <> writes:
    > KKramsch wrote:

    [...]
    >> Is there any way to implement something like C++'s try/catch
    >> mechanism for exception trapping in C?
    >> Thanks!
    >> Karl

    >
    > 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_HANDLER) {
    > exception handling block
    > }


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

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Dec 3, 2004
    #9
  10. KKramsch

    jacob navia Guest

    Dave Vandervies wrote:
    > In article <41b0b94d$0$9058$>,
    > jacob navia <> wrote:
    >
    >>KKramsch wrote:

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

    >
    >
    >>The lcc-win32 compiler implements the exception handling proposed
    >>by Microsoft for the windows system,

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


    If you had cared to *read* what the original poster wrote you would
    have seen:

    > One of the features from other languages that I miss most in C is
    > trappable exceptions.


    That was the first sentence of his post.

    > 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.


    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
     
    jacob navia, Dec 3, 2004
    #10
  11. KKramsch <> wrote:
    > In <> "Merrill & Michele" <> writes:
    > >Douglas Gwyn in news:
    > >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.


    > 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.


    You can download a nice implementation from

    http://cexcept.sourceforge.net/

    The "official" site is

    http://www.nicemice.net/cexcept/

    Alternate implementations are also listed.
     
    William Ahern, Dec 3, 2004
    #11
  12. KKramsch

    Flash Gordon Guest

    On Fri, 03 Dec 2004 22:57:31 +0100
    jacob navia <> wrote:

    > Dave Vandervies wrote:
    > > In article <41b0b94d$0$9058$>,
    > > jacob navia <> wrote:
    > >
    > >>KKramsch wrote:

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

    > >
    > >
    > >>The lcc-win32 compiler implements the exception handling proposed
    > >>by Microsoft for the windows system,

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

    >
    > If you had cared to *read* what the original poster wrote you would
    > have seen:


    I'm sure Dave did read the post, since his answer, unlike yours, shows
    an understanding of what was being asked for.

    > > One of the features from other languages that I miss most in C is
    > > trappable exceptions.

    >
    > That was the first sentence of his post.


    Yes, which provided context but did not ask anything.

    > > 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.

    >
    > I do not see any propietary extensions in a construct that is widely
    > used by all windows compilers.


    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.

    > I am not claiming any copyright in
    > that either.


    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.
     
    Flash Gordon, Dec 3, 2004
    #12
  13. KKramsch

    CBFalconer Guest

    jacob navia wrote:
    > KKramsch wrote:
    >
    > > 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?

    >
    > The lcc-win32 compiler implements the exception handling proposed
    > by Microsoft for the windows system, and implemented in most windows
    > compilers

    .... 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Dec 4, 2004
    #13
  14. KKramsch

    KKramsch Guest

    In <3d6782-9er.ln1@wilbur.25thandClement.com> William Ahern <william@wilbur.25thandClement.com> writes:

    >KKramsch <> wrote:
    >> In <> "Merrill & Michele" <> writes:
    >> >Douglas Gwyn in news:
    >> >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.


    >> 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.


    >You can download a nice implementation from


    >http://cexcept.sourceforge.net/


    >The "official" site is


    >http://www.nicemice.net/cexcept/


    >Alternate implementations are also listed.


    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.
     
    KKramsch, Dec 4, 2004
    #14
  15. Friedrich Dominicus, Dec 4, 2004
    #15
  16. "KKramsch" <> wrote in message
    news:corjg6$pcv$...
    > In <3d6782-9er.ln1@wilbur.25thandClement.com> William Ahern

    <william@wilbur.25thandClement.com> writes:
    >
    > >KKramsch <> wrote:
    > >> In <> "Merrill & Michele"

    <> writes:
    > >> >Douglas Gwyn in news:
    > >> >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.

    >
    > >> 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.

    >
    > >You can download a nice implementation from

    >
    > >http://cexcept.sourceforge.net/

    >
    > >The "official" site is

    >
    > >http://www.nicemice.net/cexcept/

    >
    > >Alternate implementations are also listed.

    >
    > Great! Thanks!
    >
    > Karl


    One final word: exception trapping and what to do with setjmp are separate
    issues. MPJ
     
    Merrill & Michele, Dec 6, 2004
    #16
  17. On Fri, 03 Dec 2004 22:57:31 +0100, jacob navia wrote:

    ....

    > 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.


    The fact that you have to specify a "Windows" restriction makes it
    blatently propietary.

    Lawrence
     
    Lawrence Kirby, Dec 6, 2004
    #17
  18. In article <>,
    Lawrence Kirby <> wrote:
    >On Fri, 03 Dec 2004 22:57:31 +0100, jacob navia wrote:
    >
    >...
    >
    >> 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.

    >
    >The fact that you have to specify a "Windows" restriction makes it
    >blatently propietary (sp?).


    Bzzt.

    Off topic, prehaps (I would argue not, but that's a can of worms), but
    proprietary - defintely not. You might want to check in a dictionary.
     
    Kenny McCormack, Dec 6, 2004
    #18
  19. Kenny McCormack <> scribbled the following:
    > In article <>,
    > Lawrence Kirby <> wrote:
    >>On Fri, 03 Dec 2004 22:57:31 +0100, jacob navia wrote:
    >>> 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.

    >>
    >>The fact that you have to specify a "Windows" restriction makes it
    >>blatently propietary (sp?).


    > Bzzt.


    > Off topic, prehaps (I would argue not, but that's a can of worms), but
    > proprietary - defintely not. You might want to check in a dictionary.


    Certainly off topic. Non-standard as well. But hardly proprietary.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "Last year he disrespected me - and then he showed lack of respect."
    - Anthony Mason
     
    Joona I Palaste, Dec 6, 2004
    #19
  20. In article <cp290i$47q$>,
    Kenny McCormack <> wrote:
    >In article <>,
    >Lawrence Kirby <> wrote:
    >>On Fri, 03 Dec 2004 22:57:31 +0100, jacob navia wrote:
    >>
    >>...
    >>
    >>> 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.

    >>
    >>The fact that you have to specify a "Windows" restriction makes it
    >>blatently propietary (sp?).

    >
    >Bzzt.
    >
    >Off topic, prehaps (I would argue not, but that's a can of worms),


    Well, then, let's make all the extensions that are "commonly" implemented
    on other platforms on-topic too. How can I make popen() grab stderr
    instead of or in addition to stdout?

    > but
    >proprietary - defintely not. You might want to check in a dictionary.


    1. Of, relating to, or suggestive of a proprietor or to proprietors
    as a group

    Sounds perfectly appropriate as a description of a feature implemented
    by a few compilers on a single platform.


    dave

    --
    Dave Vandervies
    When they knock, I politely counter-proselytize, on behalf of a randomly
    selected religion. Mormons generally go along with the joke, but JWs tend to
    get combative and leave in a huff. --Omri Schwarz in the scary devil monastery
     
    Dave Vandervies, Dec 7, 2004
    #20
    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. toylet

    trapping file i/o error

    toylet, Feb 19, 2004, in forum: Perl
    Replies:
    15
    Views:
    900
    Tassilo v. Parseval
    Feb 23, 2004
  2. Guest
    Replies:
    2
    Views:
    757
  3. pythos

    Help with trapping an exception

    pythos, Aug 24, 2004, in forum: Python
    Replies:
    3
    Views:
    360
    pythos
    Aug 24, 2004
  4. randy.buchholz

    Trapping SelectedValue Exception on dd

    randy.buchholz, Aug 11, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    randy.buchholz
    Aug 11, 2008
  5. Richard
    Replies:
    2
    Views:
    115
    Richard
    Dec 17, 2006
Loading...

Share This Page