473,657 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Nov 14 '05 #1
19 2174
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).

--
Er*********@sun .com

Nov 14 '05 #2
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_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.
Nov 14 '05 #3
In article <41************ **********@news .wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> 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 dj******@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
Nov 14 '05 #4

"Eric Sosman" <er*********@su n.com> wrote in message
news:co******** **@news1brm.Cen tral.Sun.COM...
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:2Y******** ************@co mcast.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
Nov 14 '05 #5
In <5s************ ********@comcas t.com> "Merrill & Michele" <be********@com cast.net> writes:
Douglas Gwyn in news:2Y******** ************@co mcast.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.


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.
Nov 14 '05 #6
"KKramsch"
"Merrill & Michele" <be********@com cast.net> writes:
Douglas Gwyn in news:2Y******** ************@co mcast.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.


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

Nov 14 '05 #7
In <co**********@r eader1.panix.co m> KKramsch <ka************ *******@yahooPE RIODcom.invalid > 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8
jacob navia <ja***@jacob.re mcomp.fr> 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_HANDLE R) {
exception handling block
}


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

--
Keith Thompson (The_Other_Keit h) ks***@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.
Nov 14 '05 #9
Dave Vandervies wrote:
In article <41************ **********@news .wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> 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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5783
by: Babu | last post by:
Hi, I am a Perl newbie and have a doubt on Perl exception handling. My understanding regarding exception handling is execute a piece of code, if any exception occurs, handle the exception and proceed as if the error never occurred at all. So code would look like Eval { # do something }; if ($@)
12
2079
by: Andrew Chalk | last post by:
My app. sees an exception from the kernel at an obscure address, 0x7c59ba9d. When running under the VC++ 6.0 debugger, I can trap this each time it occurs. If I want to trap it in my program and just tell the program to just continue (i.e. not pass the exception up the chain) how do I do this? Any idea how I can find out what the cause of the exception is? In the call stack my program doesn't appear. MSCORWKS (which I think is .NET, the...
6
2335
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't catch the exception and keep going. I'd just get the error message and then it would shut the...
13
4470
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
5
2563
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite handy for that purpose. The only problem when dealing with f.p. exception signals is that there is (afaik) no specification *when* the f.p. exception is raised, with one notable exception: 'feraiseexcept(int)' raises the exceptions passed in the...
2
1864
by: steven chong | last post by:
Hi, A button event on my page actually triggers a chain of method calls, and each of these methods has its own try catch statement. The error is thrown all the way from DataAccess->Business->and subsequently handled in the Presentation layer. My problem is, if i do a try catch, the inner exception of the exception which i get in the presentation layer only shows what happened in presentation. The bus, and presentation exceptions...
2
1279
by: | last post by:
I am looking for a easy way to trap an Exception that may occurr in any one of 30 child web controls to be trapped at the topmost parent WebUser control or Page Level. I have tried variations of setting the Error event handler for the topmost parent WebUser control that I want to handle the error to no avail. Sample code from the MSDN seems to imply that this can be done but it can only be taken care of at the same user control level as...
4
1280
by: CharlesA | last post by:
Hi folks, I'm working on code from Herb Schildt's book in which he says that an exception can be thrown by one method and caught by another class ExcTest { public static void genException() { int nums = new int; Console.WriteLine("Before exception is generated");
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.