2016-03-19 127 views
0

嗨,大家好,我的程序並沒有真正捕獲任何錯誤,即當我輸入一個字母而不是有效數字時,它確實發現錯誤,但它不返回菜單,它只顯示語句。當我在switch語句之外使用一個數字,即5時,它只是循環回菜單而不顯示錯誤。我的代碼如下:如何使異常處理中的程序捕獲錯誤 - Java

public void runMenu() { 
    Scanner Option = new Scanner (System.in); 
    int x = 1; 
    int Choice = 0; 
    do{ 
    try{ 
      System.out.println("Choose Option"); 
      System.out.println(""); 
      System.out.println("1: Create Account"); 
      System.out.println("2: Check Account"); 
      System.out.println("3: Take Action"); 
      System.out.println("4: Exit"); 

     System.out.println("Please choose"); 
     Choice= Option.nextInt(); 

    switch (Choice) //used switch statement instead of If else because more effective 
    { 
    case 1: 
     CreateAccount(); 
     break; //breaks iteration 
    case 2: 
     selectAccount(); 
     break; 
    case 3: 
     Menu(); 
     int choice = UserInput(); 
     performAction(choice); 
     break; 
    case 4: 
     System.out.println("Thanks for using the application"); 
     System.exit(0); 
    default: 
     throw new Exception(); 
    // x=2; //if code doesn't run successfully then x !=2 leading to exception 
} 
} 
+1

爲什麼它應該返回菜單以防萬一拋出異常?你在catch塊中寫了一個'return'語句 – Rehman

+0

John - 請注意,你最後編輯的問題已經使代碼不可編譯......並且一旦你修復了編譯錯誤,行爲將會不同。如果你想讓人們幫助你,不要做那種事。 –

回答

2

案例4沒有關閉中斷,因此你永遠不會實例化你的異常!

你應該在你的交換機年底有這樣的:

default: 
    throw new Exception(); 
    break; 

此外,你需要刪除從閉鎖段的回報。

catch (Exception e){ 
     System.err.println("Enter Correct Input"); 
     return ; 
    } 
+0

此外,你應該刪除返回 – Learner

+0

我刪除了從捕捉部分的回報,它只是一個瘋狂的一個。它不停止循環:s – John

+0

這是隻有當我輸入字母它工作正常時,它的一個不正確的號碼 – John

0

這實際上並不是通常拋出異常的情況。如果您希望程序只是在用戶輸入無效選項時循環回菜單,那麼您只需要在while循環中添加一個默認情況。你甚至不需要你的x整數。如果需要,您可以嘗試在默認情況下拋出一個新的Exception。

import javax.swing.*; 
    import java.util.Arrays; 
    import java.util.Scanner; 

    public class runMenu { 
     public void runMenu() { 


      Scanner Option = new Scanner (System.in); 

      int Choice = 0; 

        System.out.println("Choose Option"); 
        System.out.println(""); 
        System.out.println("1: Create Account"); 
        System.out.println("2: Check Account"); 
        System.out.println("3: Take Action"); 
        System.out.println("4: Exit"); 



        System.out.println("Please choose"); 
        Choice= Option.nextInt(); 




        switch (Choice) //used switch statement instead of If else because more effective 
        { 
         case 1: 

          CreateAccount(); 
          break; //breaks iteration 


         case 2: 

          selectAccount(); 


          break; 

         case 3: 
          Menu(); 
          int choice = UserInput(); 
          performAction(choice); 

          break; 
         case 4: 
          System.out.println("Thanks for using the application"); 
          System.exit(0); 

          // x=2; //if code doesn't run successfully then x !=2 leading to exception 

          throw new Exception(); 

          break; 
         default: 
          System.out.println("Invalid option. Please try    

          again."); 

          throw new Exception(); 

          runMenu(); 

        } 
       } 

     } 

    } 
0

當您輸入比1-4其他一些你應該有一個默認選項, 爲重新運行的問題,你已經顯示錯誤消息稱runMenu()

後例如,

case 4: 
     System.out.println("Thanks for using the application"); 
     System.exit(0); 
default: 
     println "Choose 1-4"; 
     runMenu() 

希望這會有所幫助。

相關問題