// user level exceptions 

import java.io.*;
class MyException extends Exception {

	static int errorCount;
	static { errorCount = 0;}
	MyException (String msg) {
		super ("User's Exception " + msg +  " occured");
		errorCount++;
	}

}

class BoundedStack {

	private int state[];
	private int top;
	private int MAX;
	private int errorCount;
	BoundedStack (int size) {
		state = new int[size];
		top=0; // top points to current empty location
		MAX = size;
		errorCount = 0;
	}
	int pop() throws MyException {
		// try here is useless -- why?
		if (top==0) 
		  throw (new MyException ("stack underflow in pop"));
		top = top-1;
		return (state[top]);
	}
	void push(int element) throws MyException {
		if (top==MAX) 
		   throw (new MyException ("stack overflow in push"));
		state[top]=element;
		top = top+1;
	}

}

class MyExceptionDemo {

	public static void displayMenu () {
		System.out.println ("Menu: create stack-0, push--1, pop--2");
	}

	public static void main (String args[]) {

		BoundedStack b = new BoundedStack (1); // default stack of size 1
		InputStreamReader r = new InputStreamReader (System.in);
		BufferedReader br = new BufferedReader (r);
		int x, element;
	
		while (true) {
  		  try {
		    displayMenu();
		    x = Integer.parseInt (br.readLine());
		    switch (x) {
			case 0: System.out.println("size?");
		  		x = Integer.parseInt (br.readLine());
				b = new BoundedStack (x);
				break;
			case 1: System.out.println("element?");
		  		element = Integer.parseInt (br.readLine());
				b.push(element);
				break;
			case 2: System.out.println ("popped:" + b.pop());
				break;

		    }

		  }
		  catch (MyException e) {
			System.out.println("Exception!! " + e.getMessage());
			if (e instanceof MyException)
				System.out.println ("Error count:" +
					((MyException)e).errorCount);
			else System.out.println ("e was not an instance of MyException");
		  }
		  catch (IOException e) {
			System.out.println ("IO Exception!");
		  }
		  catch (NumberFormatException e) {
			System.out.println (" parseInt exception!");
		  }
		  finally {

			System.out.println ("provide proper input (ignore if no exception was thrown)");

		  }
		}
	}
}





