Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > compiler warning

Reply
Thread Tools

compiler warning

 
 
dennishancy@eaton.com
Guest
Posts: n/a
 
      08-04-2005
When compiling my application, I am getting this warning msg:

---------
warning: finally clause cannot complete normally
---------

I get this warning once for every finally clause in my code.

What does this mean? What can I do to fix this?

Thanks in advance.


Dennis Hancy
Eaton Corporation
Cleveland, OH

 
Reply With Quote
 
 
 
 
John Currier
Guest
Posts: n/a
 
      08-04-2005
Do you have a return statement in your finally block?

John
http://schemaspy.sourceforge.net

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      08-04-2005
<(E-Mail Removed)> wrote:
> When compiling my application, I am getting this warning msg:
>
> ---------
> warning: finally clause cannot complete normally
> ---------
>
> I get this warning once for every finally clause in my code.
>
> What does this mean? What can I do to fix this?


You're doing something that you shouldn't do in your finally clauses.
If you post an example of code that does this, we could offer more help.

"Completing normally" is a technical term in the Java Language
Specification. It means finishing a block of code because you run into
the end of it and have no more code to run. A block of code can also
complete ABNORMALLY by:

1. Encountering a return, break, or continue statement.
2. Throwing an exception that isn't handled within that block.

Finally blocks should generally be able to complete normally. Although
the language specification doesn't require it, it just makes sense.
Hence the warning. As an example, you might write this:

try
{
doSomeIO();
}
finally
{
cleanup();
return;
}

The problem is that the return statement prevents the finally block from
completing normally. If doSomeIO() throws an exception, that exception
will be completely lost without a trace. That's because when a finally
block completes abnormally, any exceptions are lost and the program
continues in whatever way the finally block told it to. In this case,
that means returning from a method, NOT throwing whatever exception got
you to the finally block in the first place.

There is some debate about whether a finally block should EVER complete
abnormally in case of a second error, for example. However, ever
finally block should complete normally at least as the main case,
possibly with exceptions being thrown in the oddball cases.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Harry Bosch
Guest
Posts: n/a
 
      08-04-2005
You must be using an older compiler (1.3.x?). The newer compilers
enforce the spec and do not allow return statements within a finally
block(thank god) AFAIK.

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      08-05-2005
Harry Bosch wrote:
> You must be using an older compiler (1.3.x?). The newer compilers
> enforce the spec and do not allow return statements within a finally
> block(thank god) AFAIK.


class F { void f() {try { } finally { return; }}}

Works for me.

However if I add -Xlint:finally (from 1.5 onwards), I do get a warning.

C# doesn't allow return in finally.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-05-2005
Harry Bosch wrote:
> You must be using an older compiler (1.3.x?). The newer compilers
> enforce the spec and do not allow return statements within a finally
> block(thank god) AFAIK.
>


Which specification prohibits return statements within a finally block?
Can you give a reference?

Patricia
 
Reply With Quote
 
dennishancy@eaton.com
Guest
Posts: n/a
 
      08-05-2005
My finally clauses are pretty simplistic. They look like this:

finally {
return(vRet);
}

where vRet is declared as follows.

Vector vRet = new Vector();


Thanks.


Dennis

 
Reply With Quote
 
jan V
Guest
Posts: n/a
 
      08-05-2005
> My finally clauses are pretty simplistic. They look like this:
>
> finally {
> return(vRet);
> }


But that makes no sense at all... ! Can you explain what you're trying to
achieve by this?

(and by the way, you don't need to add parentheses for a return... you make
it look like it's a method call when it's not)


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> My finally clauses are pretty simplistic. They look like this:
>
> finally {
> return(vRet);
> }
>
> where vRet is declared as follows.
>
> Vector vRet = new Vector();
>
>
> Thanks.
>
>
> Dennis
>


Here's the issue. Without seeing the rest of your code, I don't know
whether you have already taking this into account and coped with it.

When a finally clause completes abruptly, completes by throwing
something or returning, the whole try-catch-finally completes abruptly
for the same reason, losing the original completion condition.

Suppose something went really, really bad during the try clause, and an
Error was thrown. Unless you are catching and logging that Error, it
will be completely lost when the finally returns.

Patricia
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-06-2005
Patricia Shanahan <(E-Mail Removed)> writes:

> Which specification prohibits return statements within a finally
> block? Can you give a reference?


A "return" counts as abrubt completion - not normal completion (JLS
14.1). But I cannot see anywhere (14.20.2) that prohibits a finally
block from completing abruptly. However, when it does, any Throwable
that may have been thrown, or any return value from the try block, is
lost.

E.g. the following method will return 16:

public static int foo() {
try {
return 42;
}
finally {
return 16;
}
}

I'd say it's bad practice to have the finally block complete abruptly,
but it's not against the spec.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WARNING! Prosoftstore.com is a SCAM! WARNING! pentologer@gmail.com ASP .Net Web Services 0 07-08-2007 10:03 AM
warning C4267 and warning C4996 B. Williams C++ 17 10-27-2006 09:41 PM
Untraceable warning: "Warning: Use of "require" without parenthesesis ambiguous at (eval 23) line 1." Julian Mehnle Perl Misc 17 05-18-2006 11:00 AM
Warning: WARNING Charles Computer Support 7 08-16-2005 09:07 PM
Re: A code fix for MSVC warning C4267 (64-bit compatibility warning,e.g. Boost Spirit) Pete Becker C++ 0 02-10-2005 01:13 PM



Advertisments