Thread: exception from C function?

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


    Why callback can not throw exception from general programming and design perspective? Does it anti some patterns?

    Quote Originally Posted by CornedBee View Post
    What crazy thing could the callback do that it would throw?

    regards,
    George

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like I said earlier, it's technically possible to construct such a thing. It is also technically possible to build a Television with all wires un-insulated, exposing tens of kilovolts to anyone getting near it. Neither is very desirable or necessary in any way.

    If you want to continue discussing this matter, perhaps you can come up with a working example of using qsort() where the compare callback is actually doing something where it throws an exception for a valid reason.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #48
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry for the inconvenience because of my bad expression, Mats!


    Well... Change my question in another way, do you think it is anti pattern to let callback function to throw exception? (it is a general question, not regarding to qsort)

    Quote Originally Posted by matsp View Post
    Like I said earlier, it's technically possible to construct such a thing. It is also technically possible to build a Television with all wires un-insulated, exposing tens of kilovolts to anyone getting near it. Neither is very desirable or necessary in any way.

    If you want to continue discussing this matter, perhaps you can come up with a working example of using qsort() where the compare callback is actually doing something where it throws an exception for a valid reason.

    --
    Mats

    regards,
    George

  4. #49
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, not as long as it catches the exception. But the point was, for what reason would such a function throw an exception? There's very few feasible scenarios.
    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. #50
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia!


    "it catches the exception" -- I think you mean C++ callback to a C function. Not for a general case of C++ callback to a C++ function. Right?

    Quote Originally Posted by Elysia View Post
    No, not as long as it catches the exception. But the point was, for what reason would such a function throw an exception? There's very few feasible scenarios.

    regards,
    George

  6. #51
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course. If it's C++ to C++ it's perfectly fine to throw an exception and not catch it inside the callback since the behavior this is defined.
    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. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As discussed earlier, you should not call C++ code from C where the C++ code throws (and doesn't catch) - it is undefined behaviour - if it happens to work on one system, it's not guaranteed to happen in another system.

    Is that clear enough?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #53
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia and Mats,


    My question is answered.

    Quote Originally Posted by matsp View Post
    As discussed earlier, you should not call C++ code from C where the C++ code throws (and doesn't catch) - it is undefined behaviour - if it happens to work on one system, it's not guaranteed to happen in another system.

    Is that clear enough?

    --
    Mats

    regards,
    George

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