• 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

try,catch and finally blocks

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clear as mud I guess people generally use finally{} when they need to clean up irregardless then?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, finally code is always executed regardless if an exception was throw n or not.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic