• 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

meaning of return; only

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends
pleaze tell me

what is the meaning of the return;

i know if we use return int; it return integer to the inclosing method
but im using return in the catch block , which is in the main method
please tell me what it return and for whom it will return

if i remove return from catch block , it shows error message like
fin variable might not have initialised
my code is as



import java.io.*;

class ShowFile
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
String ss=args[0];

try
{
System.out.println("pravin");
fin=new FileInputStream(ss);
System.out.println("pravin2");

}

catch(FileNotFoundException e)
{
System.out.println("File Not Found");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: ShowFile file");
return;
}


do
{
System.out.println("pravin2");
i=fin.read();
if(i!=-1)
System.out.print((char)i);
}
while(i!=-1);
fin.close();


}

}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"padmaratna,"

You've been asked before. Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here. Thank you for your prompt attention!

-Marc
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your "return" means stop here and do not continue after catching the exception.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Wise Owen:

Your "return" means stop here and do not continue after catching the exception



But if you have finally block then that will be executed irrespective of return in catch. Once finally gets executed, then control returns from that method.

Regards

Naseem
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But if you have finally block then that will be executed irrespective of return in catch. Once finally gets executed, then control returns from that method.



case 1: Still return from catch clause.
try {
...
} catch (Exception e) {
return ;
}
finally {
....
}

case 2: returns from finally clause and ignore return statement in catch clause:
try {
...
} catch (Exception e) {
....
return ;
}
finally {
....
return;
}
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your case 2 is absolutely true.

But in case 1, control returns from catch but after the execution of finally.

Regards

Naseem
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic