问题
Possible Duplicate:
Java: Try-Catch-Continue?
I'm reading in information from a text file and I want my code to throw an exception if there is an invalid type being read. However, I can't figure out how to have my program continue after an exception has been found
while(input.hasNext()) {
try{
type = input.next();
name = input.next();
year = input.nextInt();
} catch(InputMismatchException ex)
{
//System.out.println("** Error: Invalid input **");
//code to make program continue
}
}
回答1:
You can either just let the thread leave the catch
block - code execution will continue after that. If you want to skip the code after the catch
block, you can use the continue
keyword within a while
loop...
If you want to retrieve a year
after retrieving the name
failed with an exception, then you will have to put a try ... catch
around each input.next...()
statement. You cannot restart execution at the point the exception has been thrown.
来源:https://stackoverflow.com/questions/9813166/how-to-continue-execution-of-a-try-catch-statement-in-java