Wednesday, 25 September 2013

Java: Try/Catch Statements: While exception is caught, repeat try statements?

Java: Try/Catch Statements: While exception is caught, repeat try statements?

Is there any way to do this?
//Example function taking in first and last name and returning the last name.
public void lastNameGenerator(){
try {
String fullName = JOptionPane.showInputDialog("Enter your full
name");
String lastName = fullname.split("\\s+")[1];
catch (IOException e) {
System.out.println("Sorry, please enter your full name separated
by a space.")
//Repeat try statement. ie. Ask user for a new string?
}
System.out.println(lastName);
I think I can use scanner for this instead, but I was just curious about
if there was a way to repeat the try statement after catching an
exception.

No comments:

Post a Comment