This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exceptions

 
Jui Mahajan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code. What actually happens to the return statement in the catch clause ? If the catch block had been executed, there would then be two return statements. So is that not an error ? Pls help..
import java.io.*;
public class Ppvg{
public static void main(String argv[]){
Ppvg p = new Ppvg();
p.fliton();
}
public int fliton(){
try{
FileInputStream din = new FileInputStream("Ppvg.java");
din.read();
}catch(IOException ioe){
System.out.println("flytwick");
return 99;
}finally{
System.out.println("fliton");
}
return -1;
}
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
only one return statement will be excuted. look at the code carefully. If an exception is thrown, and catch block is excuted, it returns (99) and end the method - after the finally block. However, if the catch block is not excuted, then the last return statemt (-1) will be reached and return the method.
if you ommit the last return statment, the code will not compile
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic