This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try catch block" Watch "try catch block" New topic

try catch block

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
public class Mine {
public static void main(String argv[]){
Mine m=new Mine();
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;

the answer is
No such file found
Doing finally
Any one can explaing to me
Srikanth Iyer
Ranch Hand

Joined: Apr 30, 2007
Posts: 52
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.
shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
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.

Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)
I agree. Here's the link:
subject: try catch block
It's not a secret anymore!