簡體   English   中英

交換機中的異常處理

[英]Exception handling in a switch

以下代碼在while循環中,然后包含一個開關:

  System.out.println("Enter in a selection.");
  System.out.println("Enter \"1\" for a default selection of die");
  System.out.println("Enter \"2\" for a custom number of sides.");
  //try the input to see if its an integer
  try {
  selection = sc.nextInt();
  } catch (NumberFormatException e){
      System.out.print("Your selection can only be an integer!");
   } 
 switch (selection){
   case1:
   ...
   break;

   case2:
   ...
   break;

   default:
   ...
   //yell at them
   continue;
  }

我已經在交換機中有一個默認選擇,這樣如果用戶輸入一個無效的數字,如4(因為只有2個案例),它會將它們帶回到循環的開頭。 所以,問題是處理異常。 上面的代碼沒有處理異常,我不知道為什么。 嘗試包含有問題的代碼。

與往常一樣,如果需要,請要求澄清。 謝謝。

你應該continue例外的,因為在走在前面做了沒有點switchselection ,如果發生異常。

} catch (InputMismatchException e){
    System.out.print("Your selection can only be an integer!");
    sc.nextLine();
    continue;
}

你現在擁有它的方式,如果發生異常,你將切換selection所持有的舊陳舊值。


但是代碼中的主要問題是nextInt()方法不會拋出NumberFormatException ,而是拋出InputMismatchException

所以你試圖抓住錯誤的例外

輸入4時未處理異常的原因是4是有效整數而nextInt()不會引發NumberFormatException異常。

你最好把switch放在try塊中。

就個人而言,我會將您的輸入(以及與之相關的try / catch塊)放在自己獨立的方法中。 返回一個布爾值(true =有效整數)或超出范圍的值(可能是“-1”,具體取決於您的程序)。

恕我直言...

掃描程序#nextInt()不會拋出NumberFormatException

Scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix) , where radix< is the default radix of this scanner.

 return the int scanned from the input
 throws InputMismatchException
         if the next token does not match the <i>Integer</i>
          regular expression, or is out of range
 throws NoSuchElementException if input is exhausted
 throws IllegalStateException if this scanner is closed

您的異常處理似乎是正確的,只有您遇到錯誤異常的問題。 請捕獲InputMismatchException和其他兩個。

例如下面:

    try {
          selection = sc.nextInt();
      } catch (InputMismatchException e){
          System.out.print("Your selection can only be an integer!");
      }

您也可以將上面的代碼放在while循環中,如下所示:

       boolean validInput = false;
       while(!validInput){
     try {
          selection = sc.nextInt();
              validInput = true;
      } catch (InputMismatchException e){
          System.out.print("Your selection can only be an integer!");
      }
       }

這將反復詢問輸入,直到輸入數字。

對於Scanner ,您可以使用hasNextInt()方法(以及其他數據類型的等價物),而不是通過異常崩潰和刻錄:

while (some_condition) {
  System.out.println("Enter in a selection.");
  System.out.println("Enter \"1\" for a default selection of die");
  System.out.println("Enter \"2\" for a custom number of sides.");
  //try the input to see if its an integer
  if (!sc.hasNextInt()) {
    System.out.println("You must enter an integer!");
    continue;
  }
  selection = sc.nextInt();

  switch (selection){
   case 1:
   ...
   break;

   case 2:
   ...
   break;

   default:
   ...
   //yell at them
   continue;
  }
}

正如Bhesh Gurung所說 ,如果你處於循環中,你應該continue使用continue回到開頭,就像你在default情況下一樣。

如果要在'switch'語句中真正處理異常,則需要擴展try-catch塊的范圍:

  System.out.println("Enter in a selection.");
  System.out.println("Enter \"1\" for a default selection of die");
  System.out.println("Enter \"2\" for a custom number of sides.");
  //try the input to see if its an integer
  try {
  selection = sc.nextInt();

  switch (selection){
   case1:
   ...
   break;

   case2:
   ...
   break;

   default:
   ...
   //yell at them
   throw new NumberFormatException("Yelling message");
   continue;
  }
 } catch (NumberFormatException e){
      System.out.print("Your selection can only be an integer!");
 } 

暫無
暫無

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

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