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
posted
0
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
posted
0
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
posted
0
Thanks Gautaum, but thats just what I said..lol.
Thiru Thangavelu
Ranch Hand
Joined: Aug 29, 2001
Posts: 219
posted
0
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
posted
0
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
posted
0
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() .