Thread: STL sort throw exception?

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

    STL sort throw exception?

    Hello everyone,


    In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector.

    In what situation will sort throw exception? I can not find a case.


    thanks in advance,
    George

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Good!

    A search in Mingw implementation of algorithms didn't turn up any instances of "throw".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is possible that this wording is for precautions only - allowing sort to throw an exception allows implementations to CHOOSE whether they throw an exception under some circumstances or not, whilst stating that it can't limits the implementation to only such ways that can not throw an exception. Just like there are plenty of compilers that allow things that are undefined by the C or C++ standard - because that particular compiler or C runtime has a suitable solution for this undefined behaviour.

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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I haven't looked at that passage, but he could very well be referring to an exception being thrown while sort is being run, not necessarily that the sort function itself throws an exception.

    A simple example is if the custom sorting algorithm you pass to sort can throw an exception, or if the copy assignment operator for your class throws an exception.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Assignment operators are allowed to throw, and vector sorting definitely involves doing that, therefore it has to be able to throw. Otherwise you'd get a partially sorted vector as a result and no notification that it is so.

    However it probably doesn't actually throw new exceptions itself, but rather it would catch and rethrow any user-thrown exceptions. If your operators don't throw, then the sort shouldn't throw.
    I think this all answers the original question as well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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


    In his book, Appendix E (Standard-Library Exception Safety), (E.1 Introduction), he mentioned it (in the 1st sample).

    Quote Originally Posted by anon View Post
    Good!

    A search in Mingw implementation of algorithms didn't turn up any instances of "throw".

    regards,
    George

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


    1.

    What is the difference between *an exception being thrown while sort is being run* and *sort function itself throws an exception*? Could you show some pseudo code please?

    Quote Originally Posted by Daved View Post
    I haven't looked at that passage, but he could very well be referring to an exception being thrown while sort is being run, not necessarily that the sort function itself throws an exception.
    2.

    Quote Originally Posted by Daved View Post
    A simple example is if the custom sorting algorithm you pass to sort can throw an exception, or if the copy assignment operator for your class throws an exception.
    Agree.


    regards,
    George

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Agree, iMalc! Great!


    Quote Originally Posted by iMalc View Post
    Assignment operators are allowed to throw, and vector sorting definitely involves doing that, therefore it has to be able to throw. Otherwise you'd get a partially sorted vector as a result and no notification that it is so.

    However it probably doesn't actually throw new exceptions itself, but rather it would catch and rethrow any user-thrown exceptions. If your operators don't throw, then the sort shouldn't throw.
    I think this all answers the original question as well.

    regards,
    George

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


    I do not agree it is for precautions, but I think he means assignment operator (e.g. when doing swap) may throw exception.

    Quote Originally Posted by matsp View Post
    It is possible that this wording is for precautions only - allowing sort to throw an exception allows implementations to CHOOSE whether they throw an exception under some circumstances or not, whilst stating that it can't limits the implementation to only such ways that can not throw an exception. Just like there are plenty of compilers that allow things that are undefined by the C or C++ standard - because that particular compiler or C runtime has a suitable solution for this undefined behaviour.

    --
    Mats

    regards,
    George

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    1.

    What is the difference between *an exception being thrown while sort is being run* and *sort function itself throws an exception*? Could you show some pseudo code please?
    Typically when you call sort you provide a callback function, no?
    What Dave is referring to is that your callback function can throw an exception.

    Also, 4 posts in a row. Yeah, like a real spammer
    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
    Jan 2005
    Posts
    7,366
    Code:
    void bar(int i)
    {
        if (i == 0)
            throw std::exception();
    
        // do work
    }
    
    void foo_1(int i)
    {
        bar(i);
    }
    
    void foo_2(int i)
    {
        if (i == 0)
            throw std::exception();
        bar(i);
    }
    If you call foo_1 then *an exception will be thrown while foo_1 is being run* but if you call foo_2 then the *foo_2 function itself throws an exception*.

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


    My question is answered.

    Quote Originally Posted by Daved View Post
    Code:
    void bar(int i)
    {
        if (i == 0)
            throw std::exception();
    
        // do work
    }
    
    void foo_1(int i)
    {
        bar(i);
    }
    
    void foo_2(int i)
    {
        if (i == 0)
            throw std::exception();
        bar(i);
    }
    If you call foo_1 then *an exception will be thrown while foo_1 is being run* but if you call foo_2 then the *foo_2 function itself throws an exception*.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lifetime of temporary during exception throw
    By brewbuck in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2009, 04:08 PM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Developing Custom Exception Handler :: C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2002, 04:21 PM