• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Unreachable Code in try/catch ? why????

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

How come the return statement in the finally block is unreachable? My only guess is that because in the try block, it returns, and in the catch block, it also returns.... so it will return something and end the method before it has a chance to get to that code?
Is my thinking right on this? And this is why the compiler won't accept it?
ps. I do know that if the return was inside the finally block, that the finally 'return' would override the other returns.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Brett


My only guess is that because in the try block, it returns, and in the catch block, it also returns.... so it will return something and end the method before it has a chance to get to that code?
Is my thinking right on this? And this is why the compiler won't accept it?
[/QOUTE]
Yes, you're right.
Jamal Hasanov
www.j-think.com

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,I think you are missing something,the return statement is not in the finally block,if it was in the finally block then if wouldn't have caused a compile time error.The return statement of the finally block would have executed.

This code compiles fine!
 
Brett Swift
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gautaum, but thats just what I said..lol.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which return statement will be executed before returning to the calling method? The one in catch or finally? What is the execution order in both the cases with exception and without?
Thanks
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The programme will return either from try block(in case there is no exception) or from the catch block( in case of exception ). The finally block gets no chance to execute.
regards,
Guoqiao

Originally posted by Thiru Thangavelu:
which return statement will be executed before returning to the calling method? The one in catch or finally? What is the execution order in both the cases with exception and without?
Thanks

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

The finally block gets no chance to execute.


That can't be right. The finally block always executes regardless of what's in the try/catch blocks, except for a System.exit() .
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test it yourself. finally wins:
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic