• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brett Swift
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • 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
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Gautaum, but thats just what I said..lol.
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • 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
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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() .
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test it yourself. finally wins:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic