Wenn Sie input.nextLine() als Teil Ihrer Exception-Catch aufrufen, ist es besser, sie in jeden catch-Block oder einfach in einen finally-Block zu platzieren Am Ende des Versuches. Was ist der Vor- oder Nachteil beider Ansätze?Java-Exception-Behandlung für Scanner-Eingabe: Löschen von ungültigen Daten mit nextLine (catch vs Finally)
Beispiel:
catch (InputMismatchException e)
{
input.nextLine();
System.out.println("Cannot add to Database, please enter only integers");
}
catch (Exception e)
{
input.nextLine();
System.out.println("Impossible to add, please enter only letters from a-z");
}
Oder
catch (InputMismatchException e)
{
System.out.println("Cannot add to Database, please enter only integers");
}
catch (Exception e)
{
System.out.println("Impossible to add, please enter only letters from a-z");
}
finally
{
input.nextLine();
}
Was ist der Vorteil oder Nachteil entweder Ansatz?
Grüße,
Pille.