• 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

Return statement

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, pls check out the following code

class Test{

static String raid(){
try
{
throw new Exception();
}
catch (Exception e)
{
int i = 1 / 0 ;
return "Error";
}
finally
{
return "Peace";
}
}

public static void main(String args[]){
System.out.println(raid());
}
}


Questions:
1) what will happen to the exception(1/0) raised inside the catch block
2) is it is more than enough to have a return statement in a finally block
( i think so bcos any way it will get executed -- just to confirm)

thanks for the reply
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you return from a method from inside a finally block, any pending exceptions are discarded. The return in the finally will always be executed, and the return value of the function will be what is returned from the finally, even if the try or catch had already returned a value.

Many people consider this a flaw in the Java language definition -- it can certainly lead to some surprising behavior. You should never return from a finally -- it's a very bad practice.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
You should never return from a finally -- it's a very bad practice.


...or throw an exception.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:

...or throw an exception.



Or throw an exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic