未报告的异常StackEmptyException;必须被抓住或宣布被抛出

时间:2016-08-14 01:02:37

标签: java exception-handling

我无法弄清楚为什么这段代码的push()和pop()方法不再有效(是否正确声明了异常?)。在此先感谢您的帮助。

以下是代码:

/*
* From Schildt's Java: A Beginner's Guide, 6th ed.
* Page 628-629, 637-638
*/

// An exception for stack-full errors
class StackFullException extends Exception {
  int size;

  StackFullException(int s){
    size = s;
  }

  public String toString(){
    return "\nStack is full.  Maximum size is " + size;
  }
}// end StackFullException

// An exception for stack-empty errors
class StackEmptyException extends Exception {
  public String toString(){
    return "\nStack is empty.";
  }
}// end StackEmptyException

// A stack class for characters
class Stack {
  private char stack[]; // this array holds the stack
  private int tos; // top of stack

  // Construct an empty Stack given its size
  Stack(int size){
    stack = new char[size]; // allocate memory for stack
    tos = 0;
  }

  // Construct a Stack from a Stack
  Stack (Stack ob){
    tos = ob.tos;
    stack = new char [ob.stack.length];

    // copy elements
    for (int i=0; i < tos; i++){
      stack[i] = ob.stack[i];
    }
  }

  // Construct a stack with initial values
  Stack(char a[]){
    stack = new char[a.length];

    for (int i = 0; i < a.length; i++){
      try {
        push(a[i]);
      }
      catch(StackFullException exc){
        System.out.println(exc);
      }
    }
  }

  // Push characters onto the stack
  void push(char ch) throws StackFullException{
    if (tos == stack.length){
      throw new StackFullException(stack.length);
    }

    stack[tos] = ch;
    tos++;
  }

  // Pop a character from the stack
  char pop() throws StackEmptyException{
    if (tos == 0){
      throw new StackEmptyException();
    }

    tos--;
    return stack[tos];
  }
} // end Stack

// Demonstrate the Stack Class
public class StackDemo {
  public static void main(String args[]){

      // construct a 10-element empty stack
      Stack stack1 = new Stack(10);

      char name[] = {'B', 'r', 'a', 'd'};

      // construct stack from array
      Stack stack2 = new Stack(name);

      char ch = 0;
      int i;

      // put some characters into stack1
      for (i=0; i < 10; i++){
        stack1.push((char)('1' + i));
      }

      // construct stack from another stack
      Stack stack3 = new Stack(stack1);

      System.out.println();

      // show the stacks
      System.out.printf("Contents of stack1: ");
      for (i=0; i < 10; i++){
        ch = stack1.pop();
        System.out.print(ch);
      }

      System.out.println("\n");

      System.out.printf("Contents of stack2: ");
      for (i=0; i< 4; i++){
        ch = stack2.pop();
        System.out.print(ch);
      }

      System.out.println("\n");

      System.out.printf("Contents of stack3: ");
      for(i=0; i < 10; i++){
        ch = stack3.pop();
        System.out.print(ch);
      }

      System.out.println("\n");
  } // end main
} // end StackDemo

我知道我有点像菜鸟,我确实试图自己解决这个问题,所以请耐心等待你的答案。

1 个答案:

答案 0 :(得分:0)

如果方法抛出异常,方法的调用者应捕获异常或将其抛出。

在你的情况下,push和pop函数分别抛出StackFullException和StackEmptyException。因此调用者(main方法)应该捕获这些异常或将其抛出。

public static void main(String args[]) throws StackFullException ,StackEmptyException 
{
}

或围绕try catch块中的push和pop函数

try
{
ch = stack1.pop();
}
catch(StackEmptyException  e)
{
}