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

Return

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

This piece of code will not compile if line A added.
Any ideas?

[This message has been edited by Vladimir Kositsky (edited January 11, 2001).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually what is that you want from the code you have given ?
Sasidhar
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is smart to detect that return statement after finally will never be executed that's why it complains...
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B]
Hi
i have made some change to the original code
i ve removed the comment before the return inside the catch block and commneted out the retrun inside the try block
if u comment out the return inside the code will compile bcoz the return statement at the end of the try block will be used but if the program is not throwing any exception then programn will use the return inside the try block and return in exception is used if exception thrown we have to be very carefull bcoz compiler is very intelligent
If some question like this comes we have to see the question first anf then try all the possibilities of throwing an exception real problem comes when they wont specify what the error is .
Cherry

[This message has been edited by Cherry Mathew (edited January 11, 2001).]
 
Vladimir Kositsky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm totally agree with Vlad G.
The compiler is intelligent enough to find out where the return
statements are.
In your code there's a return in try, catch and finally.
So if any exception occurs the code after the finally will not
be executed.
Well Vladimir nice example to show that
the java compiler IS intelligent.


------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after exception has occured the program code in try -catch are executed
after which it is mandatory to transfer control to finally and return
is not just a statement but it is control transfering statement and compiler
knows it better hence it is never executed in such cases . And after finally
the method which gave the exception is aborted and the program flow is continued.

------------------
 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaranch.com/ubb/Forum24/HTML/007186.html
Cherry consider this sitution
in a try block there are 15 diffferent methods and a exception is generated at the 2nd line even then the whole try block will be executed and only then will the compiler move to catch block where the exceoption is called.
I am of the view that once an exception is generated then and there itself the compiler should move to the catch block and then if there is a return stattemnt then it should not execute at all
I think that problem is not the return statement but it is
-finally is the last piece of code.(try to put System .out.printl n after finally and the compiler issues same message.
Please do correct me if you find any contradiction.
--------------------------------------------------
Logic is the art of non contradictory existence
Ayn Rand -Atlas Shrugged
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunil,
U r right the compiler will give u the same message if u put any statement after the finally block bcoz in all the cases (even if exception occurs or not) the statement is not reached.
Compiler can check for these cases
I havent tried this but try writing some statement after a throw statement to throw an exception. It will give u an error statement not reached
Cherry
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic