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

看来您没有在程序中引发异常,要进行自定义异常引发

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 回答
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块将捕获的异常的模式。您可以找到处理错误情况的其他解决方案,例如,您可以将打印语句直接移动到default选项中。

于 2013-11-12T16:10:08.093 回答