C library errors -> C++ exceptions

L

Lionel B

System: Pentium 4, Win32, gcc 3.3.3 cygwin

Does anyone know of a mechanism whereby C library functions can be made
to throw a (C++) exception in case a call fails? This would obviate
having to check the results of function calls individually at the point
of call.

I would also be nice to be able to specify that (some) floating point
IEEE 754 "exceptions" throw C++ exceptions (some of the time).

I note that the glibc documentation suggests that "traps" may be
enabled to cause fp "exceptions" to generate a SIGFPE signal. This
suggests a possible workaround whereby a signal handler could catch the
SIGFPE signal and throw an exception (OTOH I have heard that signals
and C++ exceptions do not necessarilly mix well). This is academic, as
I have not found a way - perhaps it is not possible - to enable fp
traps for my system.

Any help/ideas appreciated,
 
B

Bob Hairgrove

System: Pentium 4, Win32, gcc 3.3.3 cygwin

Does anyone know of a mechanism whereby C library functions can be made
to throw a (C++) exception in case a call fails? This would obviate
having to check the results of function calls individually at the point
of call.

I would also be nice to be able to specify that (some) floating point
IEEE 754 "exceptions" throw C++ exceptions (some of the time).

I note that the glibc documentation suggests that "traps" may be
enabled to cause fp "exceptions" to generate a SIGFPE signal. This
suggests a possible workaround whereby a signal handler could catch the
SIGFPE signal and throw an exception (OTOH I have heard that signals
and C++ exceptions do not necessarilly mix well). This is academic, as
I have not found a way - perhaps it is not possible - to enable fp
traps for my system.

Any help/ideas appreciated,

To catch system-generated exceptions in your code, you need to use
C-style SEH (structured exception handling). Then, you need to wrap
the C functions in C++ functions which check the return value, then
throw something appropriate as a C++ exception.

It is possible that cygwin already does something under the covers
with SEH; check your documentation for details.
 
L

Lionel B

Bob said:
/snip/

To catch system-generated exceptions in your code, you need to use
C-style SEH (structured exception handling). Then, you need to wrap
the C functions in C++ functions which check the return value, then
throw something appropriate as a C++ exception.

For the non-floating point errors, if I'm going to write C++ wrappers
which check return values for the functions I am interested in - a
reasonable (if tedious) solution, I guess - it seems to me I shouldn't
have to get into SEH issues (a Good Thing, as far as I'm concerned - a
brief look has convinced me that trying to combine SEH with C++
exception handling would be nightmarish).
It is possible that cygwin already does something under the covers
with SEH; check your documentation for details.

Couldn't find anything relevant to my query in docs or mailing list
archives. Perhaps a re-post to the Cygwin mailing list might be in
order.

Thanks,
 

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

Forum statistics

Threads
474,266
Messages
2,571,083
Members
48,773
Latest member
Kaybee

Latest Threads

Top