EmptyStackException as a type : EmptyStackException « java.util « Java by API






EmptyStackException as a type

/*
 * Output:
 * 
stack: []
push(42)
stack: [42]
pop -> 42
stack: []
empty stack


 *  
 */

import java.util.Stack;
import java.util.EmptyStackException;

public class MainClass {

  public static void main(String args[]) {
    Stack st = new Stack();
    System.out.println("stack: " + st);
    st.push(new Integer(42));
    System.out.println("push(" + 42 + ")");
    System.out.println("stack: " + st);

    System.out.print("pop -> ");
    Integer a = (Integer) st.pop();
    System.out.println(a);
    System.out.println("stack: " + st);

    try {
      st.pop();
    } catch (EmptyStackException e) {
      System.out.println("empty stack");
    }
  }
}
           
       








Related examples in the same category