• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return statement

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remove the return statement in //1 it gives compileerror saying no return statment..

Why do we need return in it ? What are the rules for using it....tofind out whether the code is error or not

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;
}

}
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai karthik
tell me which line exacty.u were saying "//1" , i did nt find it in your code.
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karthik,

I assume it is the last "return"(at the end of the method) you are asking about.

In that case, "The method has to return something if its return type is not "void" ". and here "something" depends on what return type is.

Regards,
Mausam
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ho sorry

the last return type one
 
Amar Prasad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you have first return i.e. return 99; in catch block. So it will execute iff there is any IOException.

So you need to write return -1; as the last statement of the method.

if there is any IOException your return 99; will be executed followed by finally block. else second return statement retrun -1; will be executed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic