aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable Code in try/catch ?  why???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable Code in try/catch ?  why????" Watch "Unreachable Code in try/catch ?  why????" New topic
Author

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

Brett Swift
Ranch Hand

Joined: May 22, 2002
Posts: 61

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

Joined: Jan 08, 2002
Posts: 411
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

Joined: Apr 19, 2002
Posts: 93
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

Joined: May 22, 2002
Posts: 61
Thanks Gautaum, but thats just what I said..lol.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
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


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
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


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
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

Joined: May 05, 2000
Posts: 13974
Test it yourself. finally wins:


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreachable Code in try/catch ? why????