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

K

KRUNOPOPOVIC

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;
}
 
J

John Harrison

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 ?

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

john
 
K

KRUNOPOPOVIC

John Harrison je napisao/la:
Because there is no requirement in C++ do to so.

john

Thank you John,

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

kruno
 
J

John Harrison

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

That's right, popping an empty stack is undefined behaviour and
different vendors can do whatever they think is best.

john
 
K

KRUNOPOPOVIC

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:
 
J

John Harrison

John,

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

Kruno

Don't know, sorry.
 
K

KRUNOPOPOVIC

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:
 
J

John Harrison

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

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
 
K

Kai-Uwe Bux

John,

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

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

John Harrison je napisao/la:
[snip]

And, please don't top post.


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

John said:
Because there is no requirement in C++ do to so.

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
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top