尝试/捕捉错误

时间:2013-11-12 15:54:25

标签: java try-catch

任何人都可以解释为什么我的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");

                }
            }
}

2 个答案:

答案 0 :(得分: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();
 }

答案 1 :(得分:0)

没有任何事情发生,因为如果您没有获得属于预定义集合的数字,则不会执行任何特定操作。如果用户输入6,则不会验证任何if条件,您的方法也会顺利完成。

您需要抛出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选项。