Re: Developer poll. return within finally: I need your opinons
Duncan
Date: 03/03/04
- Next message: Andrew Thompson: "Re: HTML code for Java applets that fails gracefully?"
- Previous message: Adam: "Re: creating timeouts?"
- In reply to: Ryan Stewart: "Re: Developer poll. return within finally: I need your opinons"
- Next in thread: Ryan Stewart: "Re: Developer poll. return within finally: I need your opinons"
- Reply: Ryan Stewart: "Re: Developer poll. return within finally: I need your opinons"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 03 Mar 2004 14:11:31 +0000
On Wed, 3 Mar 2004 07:01:37 -0600, "Ryan Stewart"
<zzanNOtozz@gSPAMo.com> wrote:
><Duncan Strang> wrote in message
>news:c0bb40tkqplkvgbies1cc06j3vkktc11e6@4ax.com...
>> Hi
>>
>> I'd be interested to hear what developers think about the following
>> issue that has been doing the rounds here at work.
>>
>> Say we have the following code
>>
>> public int confused(){
>> String foo = "bar";
>> int val = -1;
>> try{
>> val = Integer.parseInt(foo);
>> }
>> catch(Exception ex){
>> ex.printStackTrace();
>> }
>> finally{
>> System.out.println("This always executes");
>> return val;
>> }
>> }
>>
>> The question is:
>>
>> What do you think about inserting a return within a finally block ?
>>
>> The language spec appears ambiguous, also different versions of the
>> compiler behave differently, for example.
>>
>> Compiling the above with j2se 1.4.1_02-b06 gives no warnings.
>>
>> Compiling with j2se 1.4.2-b28 gives the following warning
>>
>> Test.java [38:1] warning: finally clause cannot complete normally
>> }
>> ^
>> 1 warning
>> Finished Test.
>>
>> I've read various articles and specs that seem to indicate that the
>> jvm handles the stack correctly (in terms of return addresses for
>> example) but the compiler still raises the warning.
>>
>> All opinions gratefully accepted
>>
>> Cheers
>> Duncan L.Strang
>>
>I'd consider that the least preferable way to do it. Why return from a
>finally block when you can get exactly the same result by setting the
>variable within the finally and returning *after* it? It could also cause
>problems if someone less knowledgable has to maintain this code later on.
>Question for you: if you put a return statement in the try block *and* in
>the finally block, what happens? If you don't know, why would you expect
>someone less experienced to know? And if you do know, same question. The
>answer is that the finally block will be executed and that return value will
>be used. The return in the try block is basically ignored.
Ryan, calm down, I wasn't suggesting it was the correct way to do
things I was simply asking for opinions ... I guess you are a 'no'
then
Rgds
Duncan
>
- Next message: Andrew Thompson: "Re: HTML code for Java applets that fails gracefully?"
- Previous message: Adam: "Re: creating timeouts?"
- In reply to: Ryan Stewart: "Re: Developer poll. return within finally: I need your opinons"
- Next in thread: Ryan Stewart: "Re: Developer poll. return within finally: I need your opinons"
- Reply: Ryan Stewart: "Re: Developer poll. return within finally: I need your opinons"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|