File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes try,catch and finally blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "try,catch and finally blocks " Watch "try,catch and finally blocks " New topic
Author

try,catch and finally blocks

narasimha munagala
Greenhorn

Joined: Aug 12, 2007
Posts: 7
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

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
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

Joined: Jan 19, 2006
Posts: 38
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
mohan kumar r
Ranch Hand

Joined: Jan 19, 2006
Posts: 38
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

Joined: Jan 14, 2007
Posts: 114
Clear as mud I guess people generally use finally{} when they need to clean up irregardless then?

John Grath
Greenhorn

Joined: Sep 18, 2007
Posts: 15
Remember, finally code is always executed regardless if an exception was throw n or not.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try,catch and finally blocks