GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exceptions 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 "exceptions" Watch "exceptions" New topic
Author

exceptions

Jui Mahajan
Ranch Hand

Joined: Jun 02, 2003
Posts: 62
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;
}


-----jui<br />scjp1.4
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
 
GeeCON Prague 2014
 
subject: exceptions