Hi Kavita,
Lets look at what the JLS Spec says:
In the first case you have no "Hello.txt" file existing.
1. Try block is executed. We know this will throw a FileNotFoundException because "Hello.txt" doesn't exist!
2. The JLS states:
If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected
Using the quote we can say that FileNotFoundException is assignable to FileNotFoundException in the catch block. Therefore we will now execute that catch block.
3. The JLS states:
If the catch block completes abruptly for reason R, then the finally block is executed.
The reason, in your example, is --> return -1;
4. Now we run the finally block. The JLS states:
If the finally block completes normally, then the try statement completes abruptly for reason R.
Therefore we now quit by returning a -1.
-----------------------------------------------------------------
Now to take on the example of having "Hello.txt" existing.
1. Try block is executed. No exceptions will be thrown this time.
2. JLS states:
If execution of the try block completes normally, then the finally block is executed.
3. Finally block is executed. It completes normally.
4. JLS states:
If the finally block completes normally, then the try statement completes normally.
We are now past the try/catch/finally block. Therefore we now can perform the next statement.
5. Run the statement --> return 0; Which will return 0 to the calling program.
This is important stuff to understand. I will give you the link where you can see the total text from the JLS.
JLS Spec: Execution of try-catch-finally Regards,
Manfred.