Sign in to follow this  
Svenjamin

[java] Java Exceptions...

Recommended Posts

Svenjamin    153
Hello everyone, Ok, so I am doing some stuff with exceptions right now, and I am getting some weird behavior. Here's my code:
for (int lab=0; lab<10; lab++)
{
    while (labScores[lab] == -1)
    {
	try
        {
	    System.out.print("Please enter the score for lab " + lab + ": ");
	    labScores[lab] = input.nextInt();
				
	    if (labScores[lab] < 0 || labScores[lab] > maxLabScores[lab])
	    {
		System.out.println("Invalid Lab Score! Please Try Again.");
		labScores[lab] = -1;
 	    }
	}
	catch (InputMismatchException exception)
	{
	    System.out.println("Invalid Input! Please Try Again.");
	    labScores[lab] = -1;
	}
    }
}

So this is supposed to obtain 10 lab scores from the user. Each score is intialized to -1 at the beginning of the program, and the maxLabScores array is initialized to store the maximum points possible on each lab. Within the try block, if the nextInt() method doesn't throw an exception, then it checks to see if the score received is valid, and if so, goes on to the next lab. If not, then a new score must be entered. That part works great. But, if an InputMismatchException is thrown, (because some characters other than numbers were entered) it prints the invalid input message which it should. But then when it asks you to enter a new score, it goes right to the catch block again. I did some digging in the API docs, and it looks like it stores the last exception thrown by the scanner. Then, when I call nextInt() again, it still thinks that the exception hasn't been handled or something. I solved it by just creating a new scanner each time it asks for a score, but I would like to know if there is a better way to do it. I thought there would be some sort of ClearLastException method or something, but I couldn't find anything in the docs. Any ideas? Thanks, Svenjamin

Share this post


Link to post
Share on other sites
Antheus    2409
Exception means that input is broken. You cannot ignore it and move on, since it's impossible to tell how to continue. This is almost mandatory behavior for exceptions - once it happens, you cannot continue.

To quote JavaDoc:
Quote:
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.


If you get exception, it's up to you to do whatever you can to advance it. Scanner itself doesn't know how to. Unless you do that, Scanner will remain at same position, and fail for the same reason, no matter how often you try - you'll be triggering the same exception.

You have two options.

One is to pre-parse the input (hasNextInt()) and only then read. This way you can skip exception handling completely. You may need to skip invalid input manually.

The other way is to add nextLine() into exception handler, which will skip the current line, if the input is provided in line-by-line manner. You might need to do that in former approach as well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this