Exception trapping in C?

K

KKramsch

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
 
E

Eric Sosman

KKramsch said:
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).
 
J

jacob navia

KKramsch said:
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.
 
D

Dave Vandervies

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

Merrill & Michele

Eric Sosman said:
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 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
 
K

KKramsch

In said:
Douglas Gwyn in 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
 
M

Merrill & Michele

"KKramsch"
Douglas Gwyn in 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
 
D

Dan Pop

In said:
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
 
K

Keith Thompson

jacob navia said:
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*.
 
J

jacob navia

Dave said:
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
 
W

William Ahern

KKramsch said:
In said:
Douglas Gwyn in 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.
 
F

Flash Gordon

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.
That was the first sentence of his post.

Yes, which provided context but did not ask anything.
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.
 
C

CBFalconer

jacob said:
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.
 
K

KKramsch

In said:
KKramsch said:
In said:
Douglas Gwyn in 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

The "official" site is

Alternate implementations are also listed.

Great! Thanks!

Karl
 
M

Merrill & Michele

KKramsch said:
In <[email protected]> William Ahern
KKramsch said:
In <[email protected]> "Merrill & Michele"
Douglas Gwyn in 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

The "official" site is

Alternate implementations are also listed.

Great! Thanks!

Karl

One final word: exception trapping and what to do with setjmp are separate
issues. MPJ
 
L

Lawrence Kirby

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
 
K

Kenny McCormack

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

...


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

Joona I Palaste

Kenny McCormack said:
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.
 
D

Dave Vandervies

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top