• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

try catch block

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
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");
return 1;
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return 2;
}catch(IOException ioe) {
} finally{
System.out.println("Doing finally");
//return 3;
}

return 0;
}

}
[\code]
the answer is
No such file found
Doing finally
2
Any one can explaing to me
regards
rex
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IN the above code the FileNotFound exception is found and handeled by the catch method and thus the msg is printed. Then before the return statement the finally block gets excuted and then the return statement..


I hope it is clear now.......

That is if a return statment is embedded in the code inside the try or catch block, the code in the finally clause excuets before the return.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rex tony ,
When the control comes to the main method , the control will go to the amethod method, now if there is any exception raised in the try block ,it looks for appropriate catch block(FileNotFoundException), if found it will execute. Here in your code ,in catch block as you are returning (return 2; //statement) integer 2,before it returns the value it will search for the finally block to execute.
I hope you understood. give your valuable feedback.
 
reply
    Bookmark Topic Watch Topic
  • New Topic