continuing execution after an exception is thrown in java

115,247

Solution 1

Try this:

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

continue;

Solution 2

If you throw the exception, the method execution will stop and the exception is thrown to the caller method. throw always interrupt the execution flow of the current method. a try/catch block is something you could write when you call a method that may throw an exception, but throwing an exception just means that method execution is terminated due to an abnormal condition, and the exception notifies the caller method of that condition.

Find this tutorial about exception and how they work - http://docs.oracle.com/javase/tutorial/essential/exceptions/

Solution 3

If you have a method that you want to throw an error but you want to do some cleanup in your method beforehand you can put the code that will throw the exception inside a try block, then put the cleanup in the catch block, then throw the error.

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;
}

This way the try/catch block is not actually handling the error but it gives you time to do stuff before the method terminates and still ensures that the error is passed on to the caller.

An example of this would be if a variable changed in the method then that variable was the cause of an error. It may be desirable to revert the variable.

Share:
115,247

Related videos on Youtube

DannyD
Author by

DannyD

Updated on July 27, 2022

Comments

  • DannyD
    DannyD almost 2 years

    I'm trying to throw an exception (without using a try catch block) and my program finishes right after the exception is thrown. Is there a way that after I throw the exception, to then continue execution of my program? I throw the InvalidEmployeeTypeException which I've defined in another class but I'd like the program to continue after this is thrown.

        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);
        }
    
    
    }
    
  • manub
    manub about 12 years
    Don't you think this is not a good example of how to use Exceptions?
  • DannyD
    DannyD about 12 years
    This worked perfectly. I was able to take care of the error handling and continue execution
  • Ger
    Ger over 6 years
    It took me a while to appreciate why this code is 'not a good example' in the politely phrased opening comment. input.nextLine(); is never executed.