Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continuing execution after an exception is thrown in java

Tags:

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);     }   } 
like image 384
DannyD Avatar asked Mar 22 '12 17:03

DannyD


People also ask

How do you continue execution after throwing exception?

Resuming the program When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after execution of the complete program.

Is it possible to resume Java execution after exception occurs?

No. You cannot just jump back into the middle of a method.

How do you continue if exception occurs?

Keep one Try/Catch block inside For each body block (surrounded with Attach-window) and catch the exception and do the continue/iteration to the next row.

What happens after throwing an exception Java?

After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred.

How do I continue execution after an exception?

Continuing Execution After an Exception. When the debugger breaks execution because of an exception, you will see the Exception Helper, by default. If you have disabled the Exception Helper in the Options dialog box, you will see the Exception Assistant (C# or Visual Basic) or the Exception dialog box (C++).

How to loop the program after an exception is thrown in Java?

How to loop the program after an exception is thrown in java? Read the inputs and perform the required calculations within a method. Keep the code that causes exception in try block and catch all the possible exceptions in catch block (s).

What happens when you throw an exception in a method?

If you throw the exception, the method execution will stop and the exception is thrown to the caller method.

What happens when throwexceptions is true in Java?

Note: When ThrowExceptions is true, exceptions will be simply thrown by the ExceptionHandler . Note: The stack trace of the exception will also contain the Handle method of the ExceptionHandler. When an exception is not thrown by the ExceptionHandler, an UnthrownException is created for it.


2 Answers

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/

like image 55
manub Avatar answered Sep 23 '22 22:09

manub


Try this:

try {     throw new InvalidEmployeeTypeException();     input.nextLine(); } catch(InvalidEmployeeTypeException ex) {       //do error handling }  continue; 
like image 40
rs. Avatar answered Sep 23 '22 22:09

rs.