Win a copy of Hello World this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

meaning of return; only

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!