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); } }
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.
No. You cannot just jump back into the middle of a method.
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.
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.
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? 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).
If you throw the exception, the method execution will stop and the exception is thrown to the caller method.
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.
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/
Try this:
try { throw new InvalidEmployeeTypeException(); input.nextLine(); } catch(InvalidEmployeeTypeException ex) { //do error handling } continue;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With