簡體   English   中英

循環處理異常

[英]Looping for the handling exception

我可以問一個關於我的頭銜的問題。 因為我是Java的新手,所以我對程序中的異常處理有些困惑。 我需要做的是,當用戶輸入錯誤的輸入時,異常需要循環。 對於以下編碼,其僅執行ArrayOutOfBounds異常。 當我輸入字符串/符號/十進制值時為什么不執行FormatNumberException。 它僅顯示在輸出部分,而不顯示在showMessageDialog輸出中。 我嘗試使用多個異常處理Catch Multiple Java Exception,並且仍然相同。 您能幫我解決這個問題嗎? 任何答案將不勝感激。 對不起,我的英語不好。

public class TestRandomArray {

    public static void main(String[] args) {
        Random ran = new Random(); // create instant object from class Random.
        int array[] =new int[100]; //set an array variable with array size of 100
        int maxNumber = 150; //set the limit of the random number to generate

        for (int i = 0; i < array.length; i++) { 
            array[i] = ran.nextInt(maxNumber) + 1; //+1 means that the array number should not contain with 0 value. 1-150.
            //System.out.println(array[i]);                
        }
        //for(int i : array){
            //System.out.println(i + ",");  //this for method is other technique to print the list of array.
        //}        
            do {   
                    String input = JOptionPane.showInputDialog(null, "Enter index"); //Get input from user
                    int choosenIndex = Integer.parseInt(input); //change the string to int type.
                try {  
                    if(choosenIndex >= 0 || choosenIndex <= array.length)  
                        JOptionPane.showMessageDialog(null, "The value of choosen index in the array is : "
                        + array[choosenIndex]); //display the choosen index value.
                        System.exit(0);
                }catch (ArrayIndexOutOfBoundsException e){
                    if (choosenIndex < 0 || choosenIndex > array.length) {
                        JOptionPane.showMessageDialog(null,
                        "Index is out of bound. Enter from 0-99"); //display error for indexoutbound.
                    }
                }catch(NumberFormatException e){
                        JOptionPane.showMessageDialog(null, "Only integer allowed.");//display other error occured.
                }
            } while (true);
    }
}

Integer.parseInt(input);

該語句 try塊之外。 它必須在內部,以便您的程序跳入catch(NumberFormatException e)塊。

公共類TestRandomArray {

public static void main(String[] args) {
    Random ran = new Random(); // create instant object from class Random.
    int array[] =new int[100]; //set an array variable with array size of 100
    int maxNumber = 150; //set the limit of the random number to generate

    for (int i = 0; i < array.length; i++) { 
        array[i] = ran.nextInt(maxNumber) + 1; //+1 means that the array number should not contain with 0 value. 1-150.

    }

        do {   
             int choosenIndex = 0;
                String input = JOptionPane.showInputDialog(null, "Enter index"); //Get input from user

            try {  

                  Integer.parseInt(input); //change the string to int type.
                if(choosenIndex >= 0 || choosenIndex <= array.length)  
                    JOptionPane.showMessageDialog(null, "The value of choosen index in the array is : "
                    + array[choosenIndex]); //display the choosen index value.
                    System.exit(0);
            }catch (ArrayIndexOutOfBoundsException e){
                if (choosenIndex < 0 || choosenIndex > array.length) {
                    JOptionPane.showMessageDialog(null,
                    "Index is out of bound. Enter from 0-99"); //display error for indexoutbound.
                }
            }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null, "Only integer allowed.");//display other error occured.
            }
        } while (true);
}

}

請僅進行以下更改,它會起作用,

  int choosenIndex = 0;
            do {   
                    String input = JOptionPane.showInputDialog(null, "Enter index"); //Get input from user
                try {  
                     choosenIndex = Integer.parseInt(input); //change the string to int type.
                    if(choosenIndex >= 0 || choosenIndex <= array.length)  
                        JOptionPane.showMessageDialog(null, "The value of choosen index in the array is : "
                        + array[choosenIndex]); //display the choosen index value.
                        System.exit(0);
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null, "Only integer allowed.");//display other error occured.
                }catch (ArrayIndexOutOfBoundsException e){
                    if (choosenIndex < 0 || choosenIndex > array.length) {
                        JOptionPane.showMessageDialog(null,
                        "Index is out of bound. Enter from 0-99"); //display error for indexoutbound.
                    }
                }
            } while (true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM