poping empty stack in visual c++ express - doesn't throw exception ?

Discussion in 'C++' started by KRUNOPOPOVIC, Mar 5, 2007.

  1. KRUNOPOPOVIC

    KRUNOPOPOVIC Guest

    Hello,

    This code give me "deque empty before pop" error
    with visual c++ express in debug configuration mode.

    Why poping empty stack doesn't throw exception ?

    Uncommenting the line solves the problem.

    Kruno


    #include<iostream>
    #include<stack>
    using namespace std;

    int main()
    {
    stack<int> s ;

    try
    {
    //if (s.empty()) throw "something";
    s.pop();
    }
    catch(...)
    {
    cout<< "hello empty stack" << endl;
    }

    return 0;
    }
     
    KRUNOPOPOVIC, Mar 5, 2007
    #1
    1. Advertisements

  2. Because there is no requirement in C++ do to so.

    john
     
    John Harrison, Mar 5, 2007
    #2
    1. Advertisements

  3. KRUNOPOPOVIC

    KRUNOPOPOVIC Guest

    John Harrison je napisao/la:
    Thank you John,

    means it is proprietary MS solution,
    and other c++ compiler vendors doesn't have to follow that?

    kruno
     
    KRUNOPOPOVIC, Mar 5, 2007
    #3
  4. That's right, popping an empty stack is undefined behaviour and
    different vendors can do whatever they think is best.

    john
     
    John Harrison, Mar 5, 2007
    #4
  5. KRUNOPOPOVIC

    KRUNOPOPOVIC Guest

    John,

    Where can I get more information such as "popping an empty stack is
    undefined behaviour" ?
    Can I use Stroustrup for that ?

    Kruno



    John Harrison je napisao/la:
     
    KRUNOPOPOVIC, Mar 5, 2007
    #5
  6. Don't know, sorry.
     
    John Harrison, Mar 5, 2007
    #6
  7. KRUNOPOPOVIC

    KRUNOPOPOVIC Guest

    John,

    To be precise, where can I find more about situations always throwing
    exception in c++ ?
    How did your answer me about stack - from experience or some book ?

    Thank you very much,
    Kruno


    John Harrison je napisao/la:
     
    KRUNOPOPOVIC, Mar 5, 2007
    #7
  8. From experience. There are almost no exceptions required to be thrown
    by the standard C++ library. vector::at throws an exception, several of
    the string methods throw exceptions, new can throw an exception, but
    that is just about all.

    Doing almost any bad thing (like popping an empty stack) is undefined
    behaviour.

    john
     
    John Harrison, Mar 5, 2007
    #8
  9. KRUNOPOPOVIC

    Kai-Uwe Bux Guest

    The C++ standard is the ultimate reference for this kind of information. It
    does not teach you how to program, but it defines the language and terms
    like "undefined behavior".

    [snip]

    And, please don't top post.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 5, 2007
    #9
  10. KRUNOPOPOVIC

    KRUNOPOPOVIC Guest

    Very nice answer John,
    have a good day

    Kruno

    John Harrison je napisao/la:
     
    KRUNOPOPOVIC, Mar 5, 2007
    #10
  11. KRUNOPOPOVIC

    Kai-Uwe Bux Guest

    To be more precise, there is a requirement not to throw:

    [23.1/10]: no erase(), pop_back() or pop_front() function throws an
    exception.

    [23.2.3.3/1] defines pop() as equivalent to:

    void pop() { c.pop_back(); }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 5, 2007
    #11
  12. KRUNOPOPOVIC

    Default User Guest

    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
     
    Default User, Mar 5, 2007
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.