Arrays 即使捕捉到错误ArrayIndexOutOfBounds,如何继续循环?

Arrays 即使捕捉到错误ArrayIndexOutOfBounds,如何继续循环?,arrays,stack,Arrays,Stack,如果top==size,它将捕获一个错误并显示消息堆栈已满,而不停止循环。如何做到这一点?但我下面的代码不会停止 这是我的密码 while(ask==true){ try{ String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no"); if(input.equals("yes")){ String e

如果top==size,它将捕获一个错误并显示消息堆栈已满,而不停止循环。如何做到这一点?但我下面的代码不会停止

这是我的密码

    while(ask==true){      
    try{
        String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no");
        if(input.equals("yes")){
            String element=JOptionPane.showInputDialog("Enter Element");
             int num=Integer.parseInt(element);
             top++;
          arr[top]=num;
             System.out.println("Insertion was successful:"+" "+arr[top]);
            // isFull();

        }else if(input.equals("no")){
            pop();

        }if(input.equals("exit")){
            getStack();
            top();
            ask=false;
        }
      }catch (ArrayIndexOutOfBoundsException e){
        System.out.println("full"); 
    }

}
while(ask==true) {
try {
    String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no");
    if (input.equals("yes")){
        String element=JOptionPane.showInputDialog("Enter Element");
        int num=Integer.parseInt(element);
        top++;
        if (top == size) throw new ArrayIndexOutOfBoundsException(); //solution
        arr[top]=num;
        System.out.println("Insertion was successful:"+" "+arr[top]);
        // isFull();

    } else if (input.equals("no")){
        pop();
    } if (input.equals("exit")){
        getStack();
        top();
        ask=false;
    }
} catch (ArrayIndexOutOfBoundsException e){
    System.out.println("full");
}