File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exCeption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exCeption" Watch "exCeption" New topic
Author

exCeption

kavita s. kumar
Greenhorn

Joined: Oct 26, 2001
Posts: 16
cWhat will be output if you try to compile and run the following code, but there is
no file called Hello.txt in the current directory?.
import java.io.*;
public class Mine {
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() {
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
}catch(IOException ioe) {
} finally{
System.out.println("Doing finally");
}
return 0;
}
}
1) No such file found
2 No such file found ,-1
3) No such file found, Doing finally, -1
4) 0
Ans is 3..
an anybody let me know what happens to return 0 ??
Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
Hi,
I think that if :
FileInputStream dis=new FileInputStream("Hello.txt");
will not give any exception then finally will be executed
System.out.println("Doing finally");
and then the method will do the last line (return 0 ;). This "0" will be printed by:
System.out.println(m.amethod());
..Cristian

[This message has been edited by Marilyn deQueiroz (edited November 01, 2001).]
Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
BTW,
I didn't added the smiling face, weird...
Cristian
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Kavitha,
since you have said that there is no "Hello.txt" in the current directory,when you run the pgm you get a FileNotFoundException due to which after executing finally it returns.However,if you have "Hello.txt" in the directory,then you get the output as
Doing finally
0
Rashmi
kavita s. kumar
Greenhorn

Joined: Oct 26, 2001
Posts: 16
I cheked the ode ans is the same.. in either case how return -1 is reached..not return 0??
Also i have more question if exception is thrown in hek lass what will happen?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
 
wood burning stoves
 
subject: exCeption
 
Similar Threads
try catch block
problem with catch and finally
Exceptions
Is the sequence of output correct?
what is the concept behind?