Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
More exceptions in class Stack


Here is the additional stack specialization called HStack (for Handling Stack), in which exception handling occurs in the immediate neighborhood of each Push, Pop, and Top operation:

using System;

public class HStack: AStack{ 

   public HStack(int MaxSize): base(MaxSize){
   }
  
   public override void Push(Object el){
   try{
     base.Push(el);
   }
   catch (StackOverflowException){
     nextPush = nextPush/2;  // Effectively throwing half of the stack away!
     base.Push(el);
     Console.WriteLine("Removing half of the stack: {0}", this);
   }
  }

   public override void Pop(){
     try{
       base.Pop();
     }
     catch (StackUnderflowException){
       base.Push("El 1");  base.Push("El 2"); base.Push("El 3");
       base.Pop();
       Console.WriteLine("Adding 3 extra elements: {0}", this);
     }
  }

   public override Object Top{
     get{
       try{
         return base.Top;
       }
       catch (StackUnderflowException){
         base.Push("El 1");  base.Push("El 2"); base.Push("El 3");
         return base.Top;
       }
     }
   }

   // Empty, Full, Size and ToString are inherited

}

The class AStack (standing for ArrayStack), which is the base-class of HStack, corresponds to the solution of the previous exercise. We assume that instance variables of AStack are protected.

Notice that the version of HStack above discards the top part of the stack in case of a StackOverflowException. This is probably not what we want in a real application. It is a good exercise to remove the other half part of the stack. For this purpose, consider a new Stack operation which pops the stack in the opposite end of pushing. With such a stack, we approach a queue datatype.

Here is a sample client program, which in itself "handles" stack exceptions that remain unhandled:

using System;

class C{

  public static void Main(){

    Stack s = new HStack(5);

    try{
      Console.WriteLine("Empty: {0}", s.Empty);
      Console.WriteLine("Full: {0}", s.Full);
  
      s.Push(5); s.Push(6);  s.Push(7);      
      s.Push(15); s.Push(16);  s.Push(17); 
      s.Push(18); s.Push(19);  s.Push(20); 
      s.Push(21); 
  
      Console.WriteLine("{0}", s.Size);
      Console.WriteLine("{0}", s.Top);
      Console.WriteLine("{0}", s);
  
      s.Pop();s.Pop();s.Pop();s.Pop();s.Pop();s.Pop();s.Pop();s.Pop(); s.Pop(); s.Pop();
  
      Console.WriteLine("Empty: {0}", s.Empty);
      Console.WriteLine("Full: {0}", s.Full);
  
      Console.WriteLine("{0}", s);
      s.ToggleTop();
      Console.WriteLine("{0}", s);
   }
   catch (StackOverflowException){
     Console.WriteLine("StackOverflow");
   }
   catch (StackUnderflowException){
     Console.WriteLine("StackUnderflow");
   }

  }

}

The output of the program is as follows:

Empty: True
Full: False
Removing half of the stack: Stack: 5 6 17 
Removing half of the stack: Stack: 5 6 20 
4
21
Stack: 5 6 20 21 
Adding 3 extra elements: Stack: El 1 El 2 
Adding 3 extra elements: Stack: El 1 El 2 
Empty: True
Full: False
Stack: 
StackUnderflow