Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Exception trapping in C?

Reply
Thread Tools

Exception trapping in C?

 
 
KKramsch
Guest
Posts: n/a
 
      12-03-2004




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.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-03-2004
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).

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      12-03-2004
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.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      12-03-2004
In article <41b0b94d$0$9058$(E-Mail Removed)>,
jacob navia <(E-Mail Removed)> 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 (E-Mail Removed)
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
 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      12-03-2004

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:coqb7u$8r9$(E-Mail Removed)...
> 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:(E-Mail Removed)
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


 
Reply With Quote
 
KKramsch
Guest
Posts: n/a
 
      12-03-2004
In <(E-Mail Removed)> "Merrill & Michele" <(E-Mail Removed)> writes:
>Douglas Gwyn in news:(E-Mail Removed)
>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.
 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      12-03-2004

> "KKramsch"
> > "Merrill & Michele" <(E-Mail Removed)> writes:
> >Douglas Gwyn in news:(E-Mail Removed)
> >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



 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      12-03-2004
In <coq9mq$cmp$(E-Mail Removed)> KKramsch <(E-Mail Removed)> 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: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-03-2004
jacob navia <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      12-03-2004
Dave Vandervies wrote:
> In article <41b0b94d$0$9058$(E-Mail Removed)>,
> jacob navia <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trapping SelectedValue Exception on dd randy.buchholz ASP .Net 0 08-11-2008 03:04 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Trapping error not caught by rescue Exception -- How to? Richard Ruby 2 12-17-2006 04:01 AM
Trapping an Exception ocurring in a Webcontrol at the Page or parent WebControl level. ASP .Net 2 01-31-2005 06:34 PM
Help with trapping an exception pythos Python 3 08-24-2004 03:09 AM



Advertisments