Thread: exception from C function?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    exception from C function?

    Hello everyone,


    As far as I know, C function does not throw exception.

    But Bjarne said in his book, section 14.8 Exception and Efficiency,

    --------------------
    In particular, an implementation knows that only a few standard C library functions (such as atexit() and qsort()) can throw exceptions, and it can take advantage of that fact to generate better code.
    --------------------

    What did he mean? C function could really throw exception? What exception -- C++ exception or structured exception?


    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    atexit() and qsort() accept arguments which are functions to pointers and, from a C++ perspective, there is nothing stopping the passed functions throwing exceptions.

    Structured exceptions are a Microsoft-specific thing. There is nothing stopping a compiler targeting a windows OS from doing things with structured exceptions. But that is unrelated to Bjarne's comments.

    Bjarne was referring to the fact that, as specified, most standard C functions have no reason to throw exceptions and do not call any function that might throw exceptions. This means that, even though standard C functions have no exception specifications (which, from a C++ perspective, means they can throw ANY exception) a C++ compiler can still take advantage of the fact they won't throw exceptions.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks grumpy,


    1. If C library function qsort() takes a C++ function as callback parameter, and the C++ callback function throws exception, how could the C library function qsort() handls a C++ exception?

    2. If there are exceptions in the C++ callback function, and the C++ callback function catches the exception by itself, and never throws it again -- but returns error code to C function qsort() since C function can not handle C++ exception. If this is the case, I think it is not what Bjarne's point, since in this situation, there is no exception thrown from qsort() C library function.

    Any comments?

    Quote Originally Posted by grumpy View Post
    atexit() and qsort() accept arguments which are functions to pointers and, from a C++ perspective, there is nothing stopping the passed functions throwing exceptions.

    Structured exceptions are a Microsoft-specific thing. There is nothing stopping a compiler targeting a windows OS from doing things with structured exceptions. But that is unrelated to Bjarne's comments.

    Bjarne was referring to the fact that, as specified, most standard C functions have no reason to throw exceptions and do not call any function that might throw exceptions. This means that, even though standard C functions have no exception specifications (which, from a C++ perspective, means they can throw ANY exception) a C++ compiler can still take advantage of the fact they won't throw exceptions.

    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, perhaps, as you say, the qsort can throw an exception in that the C++ callback function it calls can throw an exception. Therefore you should take into mind that while a C function itself may never throw an exception, if it callbacks to a C++ function, the function can throw an exception because the execution is inside or beyond the qsort function. So you should keep in mind that a few C function could throw an exception (if not handled by the C++ callback function).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    I am interested in how C code could keep the status elegant (example, no resource leak and no crash) when it calls a C++ function and the C++ function throws an exception. Since C has no approach to detect whether a C++ exception is thrown, even it has no cencept of what an exception is, how C code could make the status elegant looks mysterious to me... :-)

    It is appreciated if you could give more description or recommend some learning resource (to save your time). :-)

    Quote Originally Posted by Elysia View Post
    Well, perhaps, as you say, the qsort can throw an exception in that the C++ callback function it calls can throw an exception. Therefore you should take into mind that while a C function itself may never throw an exception, if it callbacks to a C++ function, the function can throw an exception because the execution is inside or beyond the qsort function. So you should keep in mind that a few C function could throw an exception (if not handled by the C++ callback function).

    regards,
    George

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    I am interested in how C code could keep the status elegant (example, no resource leak and no crash) when it calls a C++ function and the C++ function throws an exception. Since C has no approach to detect whether a C++ exception is thrown, even it has no cencept of what an exception is, how C code could make the status elegant looks mysterious to me... :-)
    From what I see, it can't. C wasn't designed for C++ in mind, so if the C++ function throws anything, the C function is screwed if it needs to clean up anything but stack variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    Sorry for my bad English. :-)

    I do not understand how to translate "screwed" into an elegant and form term in C++ or C world. Could you express your point in some other statements please?

    Quote Originally Posted by Elysia View Post
    From what I see, it can't. C wasn't designed for C++ in mind, so if the C++ function throws anything, the C function is screwed if it needs to clean up anything but stack variables.

    regards,
    George

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You know what happens when an exception is thrown and caught outside the function. If the exception is caught in your C++ code outside qsort for example. The function never has a chance to clean up because the compiler will simply jump to your catch block without executing code in the C function, so it can't clean up. If it only uses stack variables, it will be fine, but not if it does something else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Could I understand your point in the following way?

    when there is exception in C++ callback code called from C code qsort(), runtime will looks for the handler of the exception and since C qsort() has no exception handler, it will jump to the higher address of the stack (higher than qsort()), maybe the called of the qsort() to seek for exception handlers.

    So, for local variables in C qsort(), it is ok during stack unwinding, but for other resources, there may be leak.

    My understanding correct?

    Quote Originally Posted by Elysia View Post
    You know what happens when an exception is thrown and caught outside the function. If the exception is caught in your C++ code outside qsort for example. The function never has a chance to clean up because the compiler will simply jump to your catch block without executing code in the C function, so it can't clean up. If it only uses stack variables, it will be fine, but not if it does something else.

    regards,
    George

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    You are so patient and knowable. My question is answered. :-)

    Quote Originally Posted by Elysia View Post
    Yes.

    regards,
    George

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Unfortunately, Elysia isn't quite right.

    Code compiled with a C compiler doesn't know about exceptions. It isn't prepared for exception. There absolutely no telling what will happen if you let an exception fall out of a C++ callback called from a C function. The situation is not covered by any standard: not C++, because it can't dictate terms to C code, and not C, because it isn't aware of exceptions.

    This means that the situation is undefined behaviour. It depends on how the exception handling mechanism works, and how the C implementation uses the stack. Perhaps the exception will simply fall through the C code, without cleaning up resources. Perhaps the stack unwinding will get confused and crash. Perhaps the C implementation has an extension that allows the code to clean up in the face of exceptions (perhaps SEH __finally works, for example). Perhaps the stack unwinding will get very confused and not find an exception handler even though it's there (could happen with table-based handling mechanisms) or find a handler that's totally inappropriate (could happen with stack-based handling mechanism in the face of longjmps).

    The actual rule is simple: catch all C++ exceptions at the end of a callback and use the callback's error reporting mechanism (typically a return code) to report the error. The callback doesn't have such a mechanism? (qsort's callback for example) Then there probably shouldn't be any errors. (How hard can comparing two objects be?)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee View Post
    Code compiled with a C compiler doesn't know about exceptions.
    This is completely true. It's probably worth noting, however, that Bjarne's comments were in the context of a compiler (or, more accurately, the developer of the compiler) being aware of requirements associated with C++. For example, if the C standard library is compiled from source with a C++ compiler.
    Quote Originally Posted by CornedBee View Post
    The actual rule is simple: catch all C++ exceptions at the end of a callback and use the callback's error reporting mechanism (typically a return code) to report the error. The callback doesn't have such a mechanism? (qsort's callback for example) Then there probably shouldn't be any errors. (How hard can comparing two objects be?)
    This could probably be used to support a position - in a future version of the C++ standard, for example - that callbacks passed to qsort() yield undefined behaviour if they throw an exception. The C standard need specify nothing, as no standard-compliant C code can throw an exception anyway.

    I suspect a similar argument might also be valid for atexit() callbacks. After all, in what circumstances would someone want to throw an exception in the process of exiting from a program?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    At least I nailed one thing right. I should have known it was undefined. Oh well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On a side note, Stroustrup is wrong about atexit(): since it only registers handlers and doesn't call them, it can't throw. The function that might throw is exit().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM