Corey;
Thanks again. The reason that I ask about that is that we have the following notes:
Corey
Thanks again.
The reason that I am asking about all those is that our teacher has the following slides:
example 1:
try
{
for(;
{
some action()
}
}
catch (EOFException e)
{
System.our.println()
}
System.out.println("control passed to here")
The explanantion that the teacher gives is that:
when the user input ^Z or ^D, it signals end of input and EOFException is generated. The exception is handled by the catch block and control then is passed to the last System.out.println line.
It is ok so far, but then in the example 2:
someAction() in examples 1 does not handle NumberformatException, and would result in program termination if it arose. What we should do is inform the user, but in order to continue reading the input stream, we have to nest the try and catch blocks inside the endless loop.
for(;
{
try{
someAction()
}catch{NumberFormatException e){
printout something
}
}
System.out.println("average =" +(double)total/number);
In this case, if it is true, after the exception NumberFormatException is caught, the control is returned to the endless for loop and we can have the next round of reading input again, right?
Example 3 is the same as above, only that two catch block is provided, says after EOFException is caught, the control is passed back to endless for loop , which is not what we want, therefore, a break is introduced:
for(;
{
try
{someAction()
}catch(EOFException e){
printout something
}
catch(NumberFormatException e){
printout something again
}
break;
}
System.out.println("something")
All the above is where my confusion is from.
From the above, my conclusion is that once an exception is caught by a catch block, the code control is returned to the method that calls the try/catch block and if that method is an endless for loop, the for loop will go on the its' next iteration unless some action is taking to break out the for loop, such as either a break right after the try/catch block or in the catch block, or an exception happens that was not caught.
Pleas help with me on if it is right?
Thanks
Jo