J'essaie d'exécuter plusieurs blocs try-catch dans une boucle do-while pour aider à détecter des erreurs telles que InputMissMatch pour les entrées entières et doubles, cependant , J'ai toujours besoin d'être en mesure d'entrer des chaînes dans la même boucle do-while de l'interface de la console.Try-Catch ne ré-exécute pas quand il y a du code après le bloc try-catch
Est-il possible d'obtenir que les blocs try-catch soient réexécutés après avoir attrapé une exception, au lieu de passer à la ligne de code suivante et si oui, comment?
Ceci est mon code:
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;
public class EmployeeBenefits {
Map <Integer, CaptureDetails> map = new HashMap<>();
int number;
String name;
String surname;
double grossSalary;
int employeeNumber = 1;
int numberOfEmployees = 0;
public void captureDetails() {
boolean codeIsRunning = true;
Scanner input = new Scanner (System.in);
do {
inputNumberOfEmployees();
if (numberOfEmployees != 0) {
for (int enterDetails = 0; enterDetails < numberOfEmployees; enterDetails++) {
inputEmployeeNumber();
inputEmployeeNames();
inputGrossSalary();
map.put(employeeNumber, new CaptureDetails (number, name, surname, grossSalary));
employeeNumber++;
}
}
codeIsRunning = false;
} while (codeIsRunning); // end of do-while loop
} // end of captureDetails() method
public void inputNumberOfEmployees() {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter the number of employees to input: ");
numberOfEmployees = input.nextInt();
}
catch (InputMismatchException e) {
System.out.println ("\nYou must enter an integer value. Please try agin\n");
input.nextLine();
}
} // end of inputNumberOfEmployees()
public void inputEmployeeNumber() {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter Number: ");
number = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("\nYou must enter an integer value. Please try agin\n");
input.nextLine();
}
} // end of inputEmployeeNumber()
public void inputGrossSalary() {
Scanner input = new Scanner (System.in);
try {
System.out.print ("Enter Gross Salary: ");
grossSalary = input.nextDouble();
}
catch (InputMismatchException e) {
System.out.println ("\nEnter employees salary. Please try agin\n");
input.nextLine();
}
} // inputGrossSalary()
public void inputEmployeeNames() {
Scanner input = new Scanner (System.in);
System.out.print ("Enter Name: ");
name = input.nextLine();
System.out.print ("Enter Surname: ");
surname = input.nextLine();
} // end of inputEmployeeNames
public static void main(String[] args) {
EmployeeBenefits eb = new EmployeeBenefits();
boolean programIsRunning = true;
Scanner input = new Scanner (System.in);
while (programIsRunning) {
System.out.println ("1. Enter Employee details");
System.out.println ("2. Set New Salary");
System.out.println ("3. Print Employee Details");
System.out.println ("4. Exit");
switch (input.nextInt()) {
case 1:
eb.captureDetails();
break;
case 2:
break;
case 3:
break;
case 4:
programIsRunning = false;
break;
default :
System.out.println ("Enter a value between from 1 to 4\n");
} // end of switch-case (input.nextInt())
} // end of while (programIsRunning) loop
} // end of main method
} // end of EmployeeBenefits class
class CaptureDetails {
int number;
String name, surname;
double grossSalary;
public CaptureDetails (int number, String name, String surname, double grossSalary) {
this.number = number;
this.name = name;
this.surname = surname;
this.grossSalary = grossSalary;
}
} // end of CaptureDetails class
Vous pouvez placer un try-catch dans la boucle. Est-ce ce que vous demandez? –
Est-il possible de relancer les blocs try-catch après avoir attrapé une exception, au lieu de passer à la ligne de code suivante et si oui, comment? – Yeezus
Essayez quelque chose comme ceci: https://stackoverflow.com/questions/42282638/exception-handling-with-a-do-while-loop-in-java –