posted 20 years ago
Hi,
What is happening is as follows:
Your method m is throwing an exception in the try block, which is caught the catch block.
This catch block again rethrows the exception.
The rule for using a method which throws a checked exception is that either it should declare in its signature that it throws an exception or it should handle the exception within its body, and effectively not throw the exception. Now you are catching the exception and rethrowing it, so the control goes the finally block before coming out of the method.
In the finally block, by putting a return statement, you have ignored the exception that you threw in the catch block, and are providing a normal return value to be printed. Hence this value, 3 , will be printed.
Now for the second part of the question.
Had the return been put outside the finally block, as is the normal/best practice, it would have given an error saying statement not reachable because the control moves out of the method after the finally block.So the last return statement would never be reached because of the abnormal termination due to the exception you are throwing in the catch block.
Finally, I am not getting any warnings with your programs. Only errors for the conditions you said. Maybe because of different jdk versions.
Neways, I hope the flow is clear now.
Cheers
Anupreet