#include <iostream>
#include "iStack.h"
int main() {
iStack stack( 32 );
stack.display();
for ( int ix = 1; ix < 51; ++ix )
{
if ( ix % 3 == 0 )
stack.push( ix );
if ( ix % 4 == 0 )
stack.display();
if ( ix % 10 == 0) {
int dummy;
stack.pop( dummy );
stack.display();
}
}
return 0;
}
for ( int ix = 1; ix < 51; ++ix ) {
try { // try block for pushOnFull exceptions
if ( ix % 3 == 0 )
stack.push( ix );
}
catch ( pushOnFull ) { ... }
if ( ix % 4 == 0 )
stack.display();
try { // try block for popOnEmpty exceptions
if ( ix % 10 == 0 ) {
int dummy;
stack.pop( dummy );
stack.display();
}
}
}
catch ( popOnEmpty ) { ... }
try {
for ( int ix = 1; ix < 51; ++ix )
{
if ( ix % 3 == 0 )
stack.push( ix );
if ( ix % 4 == 0 )
stack.display();
if ( ix % 10 == 0 ) {
int dummy;
stack.pop( dummy );
stack.display();
}
}
}
catch ( pushOnFull ) { ... }
catch ( popOnEmpty ) { ... }
main() returns 0.push() member function called within the first if statement of the for loop throws an exception, the second and third if statements of the for loop are ignored, the for loop and the try block are exited, and the handler for exceptions of type pushOnFull is executed.pop() member function called within the third if statement of the for loop throws an exception, the call to display() is ignored, the for loop and the try block are exited, and the handler for exceptions of type popOnEmpty is executed.int main() {
try {
iStack stack( 32 ); // ok: declaration in try block
stack.display();
for ( int ix = 1; ix < 51; ++ix )
{
// same as before
}
}
catch ( pushOnFull ) {
// cannot refer to stack here
}
catch ( popOnEmpty ) {
// cannot refer to stack here
}
// cannot refer to stack here
return 0;
}
int main() {
try {
iStack stack( 32 );
stack.display();
for ( int ix = 1; ix < 51; ++ix )
{
// same as before
}
return 0;
}
catch ( pushOnFull ) {
// cannot refer to stack here
}
catch ( popOnEmpty ) {
// cannot refer to stack here
}
}