This program is trying to read a file whose name is provided at command line.
And if the file is not found a FileNotFoundException is thrown.
I have undersood this much only.
What I am not getting is if once the exception has caught execution terminates and message is displayed given inside println keyword.
Then What the keyword "return" is doing there.
I know , I am wrong somwhere or I've misunderstood the concept, but where am i getting wrong ?
Please help me in geeting right concept.
posted 8 years ago
When an exception is thrown and caught, execution doesn't terminate. It just transfers to the catch block. Unless you do something in there to stop it (like rethrowing the exception or having a return statement) execution will then continue after the try/catch block. Without the return statement this program would go on to try and read from the file it just failed to find.
Normally you wouldn't return from inside a catch block. They probably just did it here to keep this small demo program short and simple.
In a real program, if you catch an exception, you usually need to either actually handle it (such as retrying, or using a valid default value), or else rethrow it (usually wrapped in a more layer-appropriate exception. Otherwise, the caller doesn't know something went wrong. He thinks the method succeeded and everything is fine, so he goes on as if that's the case. We usually don't want that.