Site hosted by Angelfire.com: Build your free website today!
 

Throwing Exeception in your Methods

package simplestack;

public class SimpleStack {
    public static final int STKSIZE = 4;
    protected Object[] stack = new Object[STKSIZE];
    protected int top = 0;
    public void push(Object obj) throws StackOverflowException {
       try {
           stack[top++] = obj;
       } catch (Exception e) { // IndexArrayOutOfBoundsException
           top = STKSIZE;
           throw new StackOverflowException(); // rethrows as ...
       }
    }
    public Object pop() throws StackUnderflowException {
       try {
           return stack[--top];
       } catch (Exception e) { // IndexArrayOutOfBoundsException
           top = 0;
           throw new StackUnderflowException(e.getMessage());
       }
    }
    public int top() {
        return top;
    }
}