This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try,catch and finally blocks

 
narasimha munagala
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to everyone
I am new to java i want to know
in all the try ,catch,finally blocks return statements exists
if an exception raised in the try block then which return
statement execute
and if an exception not raised in the try block then
which return stetment execute.
Any one know answer for this question please give me answer
thank you
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narasimha munagala,
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
CarefullyChooseOneForum

The JavaRanch forums are divided by category.
You stand a much better chance of getting a good answer if you ask your question in the right one.

I'll move this to Java In General (Intermediate) for you.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain what the difference between using "finally" and just putting the code after the try/catch is too?

 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this(asume there is no file abc.txt)

public class TRYCATCH {
public static void main(String[] args) {
try{
System.out.println("before call");
TRYCATCH.method();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
private static void method()
{
File f = new File("c:/abc.txt");
try{
System.out.println("===before fis====");
FileInputStream fis = new FileInputStream(f);
System.out.println("===after fis====");
return;
}
catch(Exception e){
System.out.println("===in catch====");
return;
}finally{
try{
FileInputStream fis = new FileInputStream(f);
System.out.println("===in finally====");
return;
}catch(Exception e){
System.out.println("===inside finally catch====");
}
}

}
}
 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
try this(with and witout finally block).Now the difference is obvious.

public static void main(String[] args) {
try{
System.out.println("before call");
TRYCATCH.method();

}catch(Exception e){
System.out.println(e.getMessage());
}

}
private static void method() throws Exception
{
try {
FileInputStream fis = new FileInputStream(new File("c:/abc.txt"));
}
catch (Exception e) {
throw new Exception("Foobar");
}
//finally {
System.out.println("here");
//}
}
}
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clear as mud I guess people generally use finally{} when they need to clean up irregardless then?

 
John Grath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, finally code is always executed regardless if an exception was throw n or not.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Chris Corbyn :]Can someone explain what the difference between using "finally" and just putting the code after the try/catch is too?

I snagged on this earlier also. This time the beginner with Posts == 4 gets the answer.

GIVEN:



What you get is a badly hung machine and a fouled-up mess that would be better left to recovery team.
[ September 22, 2007: Message edited by: Nicholas Jordan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic