Re: Developer poll. return within finally: I need your opinons

Duncan
Date: 03/03/04


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
>



Relevant Pages

  • Re: Developer poll. return within finally: I need your opinons
    ... <Duncan Strang> ... > issue that has been doing the rounds here at work. ... > The language spec appears ambiguous, ... > example) but the compiler still raises the warning. ...
    (comp.lang.java.programmer)
  • Re: Source maintenance was Re: SEQUENCE NUMBERS
    ... if two different developers insert similar ... result in at least a warning. ... For IBM-MAIN subscribe / signoff / archive access instructions, ... send email to listserv@xxxxxxxxxxx with the message: GET IBM-MAIN INFO ...
    (bit.listserv.ibm-main)
  • Re: [-mm patch] unexport sys_{open,read}
    ... Just how much of a warning is needed in this particular case? ... You guys keep on talking about developers. ... something which we've done, they get pissed off, and we lose a tester. ... change interfaces on those relatively rare occasions when it is possible, ...
    (Linux-Kernel)
  • Re: Developer poll. return within finally: I need your opinons
    ... > example) but the compiler still raises the warning. ... Section 14.1 of the Language Spec covers normal vs. abrupt ... unwise to write code whose correct interpretation by another human ...
    (comp.lang.java.programmer)
  • Re: Why no serious MS Application in .NET yet ??
    ... If DDE and its peripheral technology posed a constant security hole and very ... few developers use it and MS has been warning to abandon it for 10+ years, ...
    (microsoft.public.dotnet.general)