0

try/catch が機能しない理由を説明できる人はいますか? ユーザーが文字を入力するとエラーメッセージが表示されるか、オプションにない番号を入力するとエラーが表示されるようにしようとしています。文字を入力するときに通常の赤い行のエラーが表示されるだけで、たとえば数字の 6 を入力しても何も起こりません。

public void menu()
     {
            System.out.println("Welcome to your Payroll System");
            System.out.println();
            System.out.println("Please enter your choice below from the following options");
            System.out.println();
            System.out.println("View all current weekly employees = 1 ");
            System.out.println("View all current monthly employees = 2 ");
            System.out.println("Delete an employee = 3 ");
            System.out.println("Add an employee = 4 ");
            System.out.println("Print an employee payslip = 5");
            System.out.println("To exit the system = 0 ");

            // allows user to enter number of choice and this reflects which statement is ran in userChoice method
            tempvar = sc.nextInt();
            userChoice();
     }



            public void userChoice() 

            {  
               try
               {
                // if user enters 1 it prints out the employee list.
                if (tempvar == 1) 
                {
                    w.printWeekly();    
                } 
                if (tempvar == 2) 
                {
                    mo.printMonthly();
                } 
                if (tempvar == 3) 
                {
                    e.deleteEmployee();
                } 
                if (tempvar == 4) 
                {
                    e.addEmployee();
                } 
                if (tempvar == 5) 
                {
                    mo.createPayslip(); 
                }

                if (tempvar == 0) // if user hits 0 it allows them to exit the programme

                {
                    System.out.println("You have exited the system");
                    System.exit(0);
                }
            }

               catch(InputMismatchException e)
                {
                    System.out.println("Error in the data you have entered please try again");

                }

                catch(Exception e)
                {
                    System.out.println("Error in the data you have entered please try again");

                }
            }
}
4

2 に答える 2

0

事前定義されたセットのメンバーである番号を取得しない場合に備えて、具体的なことを何もしないため、何も起こりません。ユーザーがを入力すると6ifどの条件も検証されず、メソッドはスムーズに完了します。

type の例外をスローする必要があります。InputMismatchExceptionそうしないと、catchブロックは意味がありません。

私があなたなら、次のswitchような声明を書きます。

try
    {
    switch(tempvar):
        case 1:
            w.printWeekly();    
            break;
        case 2:
            mo.printMonthly();
            break;
        case 3: 
            e.deleteEmployee();
            break;
        case 4:
            e.addEmployee();
            break;
        case 5: 
            mo.createPayslip(); 
            break;
        case 0:
            System.out.println("You have exited the system");
            System.exit(0);
        default:
            throw new InputMismatchException();
        }
catch(InputMismatchException e)
{
     System.out.println("Error in the data you have entered please try again");
}

catchただし、ブロックがキャッチすることがわかっている例外をスローすることは、推奨されるパターンではありません。誤ったケースを処理するための他の解決策を見つけることができます。たとえば、print ステートメントをdefaultオプションに直接移動できます。

于 2013-11-12T16:10:08.093 に答える
0

カスタム例外を発生させるために、プログラムで例外を発生させなかったようです

throw new InputMismatchException("This exception is going to be handled elsewhere");

編集 1

例外が発生した行を try-catch ブロックで囲む必要があります。スキャナーで処理するには

で包む

 public void menu()
 {
        System.out.println("Welcome to your Payroll System");
        System.out.println();
        System.out.println("Please enter your choice below from the following options");
        System.out.println();
        System.out.println("View all current weekly employees = 1 ");
        System.out.println("View all current monthly employees = 2 ");
        System.out.println("Delete an employee = 3 ");
        System.out.println("Add an employee = 4 ");
        System.out.println("Print an employee payslip = 5");
        System.out.println("To exit the system = 0 ");

        // allows user to enter number of choice and this reflects which statement is ran in userChoice method
        try {
             tempvar = sc.nextInt();
        } catch (InputMismatchException e) {
             System.out.println("...");
        }
        userChoice();
 }
于 2013-11-12T15:58:55.027 に答える