【问题标题】:continuing execution after an exception is thrown in java在java中抛出异常后继续执行
【发布时间】:2012-04-07 07:13:57
【问题描述】:

我正在尝试抛出异常(不使用 try catch 块),并且我的程序在抛出异常后立即完成。有没有办法在我抛出异常后继续执行我的程序?我抛出了我在另一个类中定义的 InvalidEmployeeTypeException,但我希望程序在抛出后继续。

    private void getData() throws InvalidEmployeeTypeException{

    System.out.println("Enter filename: ");
    Scanner prompt = new Scanner(System.in);

    inp = prompt.nextLine();

    File inFile = new File(inp);
    try {
        input = new Scanner(inFile);
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        System.exit(1);
    }

    String type, name;
    int year, salary, hours;
    double wage;
    Employee e = null;


    while(input.hasNext()) {
        try{
        type = input.next();
        name = input.next();
        year = input.nextInt();

        if (type.equalsIgnoreCase("manager") || type.equalsIgnoreCase("staff")) {
            salary = input.nextInt();
            if (type.equalsIgnoreCase("manager")) {
                e = new Manager(name, year, salary);
            }
            else {
                e = new Staff(name, year, salary);
            }
        }
        else if (type.equalsIgnoreCase("fulltime") || type.equalsIgnoreCase("parttime")) {
            hours = input.nextInt();
            wage = input.nextDouble();
            if (type.equalsIgnoreCase("fulltime")) {
                e = new FullTime(name, year, hours, wage);
            }
            else {
                e = new PartTime(name, year, hours, wage);
            }
        }
        else {


            throw new InvalidEmployeeTypeException();
            input.nextLine();

            continue;

        }
        } catch(InputMismatchException ex)
          {
            System.out.println("** Error: Invalid input **");

            input.nextLine();

            continue;

          }
          //catch(InvalidEmployeeTypeException ex)
          //{

          //}
        employees.add(e);
    }


}

【问题讨论】:

    标签: java exception-handling throw


    【解决方案1】:

    如果抛出异常,方法执行将停止,异常被抛出给调用者方法。 throw 总是中断当前方法的执行流程。 try/catch 块是你在调用可能抛出异常的方法时可以写的东西,但是抛出异常仅仅意味着方法执行由于异常情况而终止,并且异常通知调用者方法那个条件。

    查找有关异常及其工作原理的教程 - http://docs.oracle.com/javase/tutorial/essential/exceptions/

    【讨论】:

      【解决方案2】:

      试试这个:

      try
      {
          throw new InvalidEmployeeTypeException();
          input.nextLine();
      }
      catch(InvalidEmployeeTypeException ex)
      {
            //do error handling
      }
      
      continue;
      

      【讨论】:

      • 你不觉得这不是一个如何使用异常的好例子吗?
      • 这非常有效。我能够处理错误并继续执行
      • 我花了一段时间才明白为什么这段代码在礼貌的开场白中“不是一个好例子”。 input.nextLine(); 永远不会被执行。
      【解决方案3】:

      如果你有一个方法想要抛出一个错误,但你想事先在你的方法中做一些清理,你可以将抛出异常的代码放在 try 块中,然后将清理放在 catch 块中,然后抛出错误。

      try {
      
          //Dangerous code: could throw an error
      
      } catch (Exception e) {
      
          //Cleanup: make sure that this methods variables and such are in the desired state
      
          throw e;
      }
      

      这样,try/catch 块实际上并没有处理错误,但它让您有时间在方法终止之前做一些事情,并且仍然确保将错误传递给调用者。

      例如,如果方法中的变量发生更改,则该变量是导致错误的原因。可能需要恢复变量。

      【讨论】:

        猜你喜欢
        • 2012-03-08
        • 2015-10-31
        • 1970-01-01
        • 1970-01-01
        • 2021-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多