C library errors -> C++ exceptions

Discussion in 'C++' started by Lionel B, Oct 7, 2004.

  1. Lionel B

    Lionel B Guest

    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,

    --
    Lionel B
    Lionel B, Oct 7, 2004
    #1
    1. Advertising

  2. On 7 Oct 2004 03:13:43 -0700, "Lionel B" <> wrote:

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

    --
    Bob Hairgrove
    Bob Hairgrove, Oct 7, 2004
    #2
    1. Advertising

  3. Lionel B

    Lionel B Guest

    Bob Hairgrove wrote:
    > On 7 Oct 2004 03:13:43 -0700, "Lionel B" <> wrote:
    >
    > >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).


    /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,

    --
    Lionel B
    Lionel B, Oct 7, 2004
    #3
    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. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    927
    Mark Goldin
    Jan 17, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    29,993
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,010
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    601
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    602
Loading...

Share This Page