• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

help needed

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ExceptionDemo2 {

public static int meth() {

try {
throw new RuntimeException("ExceptionGenerated");
}catch(Exception e) {

System.out.println("HELO hoo");
return 1;
}
finally {
System.out.println("HELO boo");
return 2;
}
}
public static void main(String[] args) {

int j=meth();
System.out.println(j);
}
}

Here i am not able to understand why return statement in catch block not getting executed.
Any help???
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Here once exception raises, it reaches catch block since you have return statement in catch block, it will go to finally and execute the code, but in finally you have return statement, so it will return from finally only, it will not return to catch again.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point to remember is that, finally block will always be executed.

the return statement passes the control to finally block. Then the return statement in the finally block is executed.

The return statement in the catch block is executed, but the control is returned to finally, and then the finally block is executed. To clarify, you can remove the return statement in the finally block and then run the program.

And even quote the source for this question other wise your post will be deleted.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch. When you post a question, please use a meaningful subject line instead of something like "help needed".
 
Happily living in the valley of the dried frogs with a few tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic