Start New Discussion within our Software Development Community

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

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

struct StackFullException
{
StackFullException();
};

StackFullException::StackFullException()
{
  cout << "The stack is full.";
}

struct StackEmptyException
{
StackEmptyException();
};

StackEmptyException::StackEmptyException()
{
  cout << "The stack is empty.";
}

Here is the teststack.h

#ifndef TESTSTACK_H
#define TESTSTACK_H

class Stack
{
  int size;
  int tos;
  char* S;
public:
  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--];
  }
};

#endif

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?

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