• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

exCeption

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW,
I didn't added the smiling face, weird...
Cristian
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic