Why are my exceptions not working?
here is the teststack.cpp

#include <iostream>
#include <iomanip>
#include "teststack.h"
using namespace std;

struct StackFullException

  cout << "The stack is full.";

struct StackEmptyException

  cout << "The stack is empty.";

Here is the teststack.h


class Stack
  int size;
  int tos;
  char* S;
  struct StackFullException{};
  struct StackEmptyException{};
  Stack(int sz = 100):size(sz),tos(-1){S = new char[size];}
  ~Stack(){if (S) delete[] S;}
  bool IsFull(void){return tos >= size - 1;}
  bool IsEmpty(void){return tos < 0;}
  void Push(int v)
    if (IsFull()) throw StackFullException();
    S[++tos] = v;
  char Pop(void)
    if (IsEmpty()) throw StackEmptyException();
    return S[tos--];


Then, in main, I run an assignment loop that puts a character in 101 times. I compile the source.cpp and the teststack.cpp and link the .o files. The exception error never displays and I get a stackdump. Does anyone see what I am doing wrong?

Edited 4 Years Ago by diafol: fixed formatting

This question has already been answered. Start a new discussion instead.