Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » finally block does not complete normally
finally block does not complete normally [message #169915] Fri, 23 July 2004 18:06 Go to next message
Eclipse UserFriend
Hi,

I upgraded to Eclipse 3.0 this week and am now getting a problem warning
on all of my finally blocks in all my projects. When I check to see what
Eclipse suggests for to fix this, I get "No corrections available". As
fas as I know there is nothing wrong with my finally blocks. I've done an
internet and Eclipse search and found nothing. Is anyone else getting
these errors? Does anyone know why they're being generated? Maybe it's a
bug in Eclispe?

Thanks in advance,
Shelli
Re: finally block does not complete normally [message #169925 is a reply to message #169915] Fri, 23 July 2004 18:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.NO.SPAM.us.ibm.com

Please append your finally block so that we can see it.


--
Thanks, Rich Kulp

Re: finally block does not complete normally [message #169933 is a reply to message #169915] Fri, 23 July 2004 19:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Fri, 23 Jul 2004 22:06:29 +0000 (UTC), shelli.orton@wmode.com
(Shelli) a écrit :
>I upgraded to Eclipse 3.0 this week and am now getting a problem warning
>on all of my finally blocks in all my projects. When I check to see what
>Eclipse suggests for to fix this, I get "No corrections available". As
>fas as I know there is nothing wrong with my finally blocks. I've done an
>internet and Eclipse search and found nothing. Is anyone else getting
>these errors? Does anyone know why they're being generated? Maybe it's a
>bug in Eclispe?
Your finally block might contain a return statement. If this is the
case, the finally block doesn't complete normally. Normally means that
you exit the block at the end of the block without going through a
return or a throw statement.
Paste one of your finally block and we can tell you for sure what is
wrong.
Because a finally block is executed even if there is an uncaught
exception in the try block, a finally block that doesn't complete
normally is a potential problem or source of bugs.
For example, if you have something like this:

class Example {
static void foo() {
throw new RuntimeException();
}

public static void main(String[] args) {
try {
foo();
} finally {
return;
}
}
}

Do you think that the exception will be thrown after the finally block
is executed? The answer is no. Because you have a return statement,
the runtime exception is "eaten".

That example will throw the runtime exception:

class Example {
static void foo() {
throw new RuntimeException();
}

public static void main(String[] args) {
try {
foo();
} finally {
}
}
}

This is why we have such warning. Note this is only a warning. There
is nothing wrong doing that. It could however hide a bug.

Hope this help,
--
Olivier
Re: finally block does not complete normally [message #170030 is a reply to message #169915] Mon, 26 July 2004 11:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: bob.news.gmx.net

"Shelli" <shelli.orton@wmode.com> schrieb im Newsbeitrag
news:cds255$efu$1@eclipse.org...
> Hi,
>
> I upgraded to Eclipse 3.0 this week and am now getting a problem warning
> on all of my finally blocks in all my projects. When I check to see
what
> Eclipse suggests for to fix this, I get "No corrections available". As
> fas as I know there is nothing wrong with my finally blocks. I've done
an
> internet and Eclipse search and found nothing. Is anyone else getting
> these errors? Does anyone know why they're being generated? Maybe it's
a
> bug in Eclispe?

You probably exit a finally block via a return or via a possible
exception. This is often the case if you close an InputStream or an
OutputStream in a finally without catching the IOException. You should
always rewrite those finally blocks to

....
}
finally {
try {
stream.close();
}
catch ( IOException ignore ) {
}
}

Regards

robert
Re: finally block does not complete normally [message #170077 is a reply to message #169933] Mon, 26 July 2004 16:17 Go to previous messageGo to next message
Eclipse UserFriend
Aha!

Yes, all the finally blocks that are getting the warning have the return
statement in them. I'll have to take a closer look and make sure there's
no potential for hidden bugs and make changes as necessary.

Thank you so much!

Shelli

Olivier Thomann wrote:

> Le Fri, 23 Jul 2004 22:06:29 +0000 (UTC), shelli.orton@wmode.com
> (Shelli) a écrit :
> >I upgraded to Eclipse 3.0 this week and am now getting a problem warning
> >on all of my finally blocks in all my projects. When I check to see what
> >Eclipse suggests for to fix this, I get "No corrections available". As
> >fas as I know there is nothing wrong with my finally blocks. I've done an
> >internet and Eclipse search and found nothing. Is anyone else getting
> >these errors? Does anyone know why they're being generated? Maybe it's a
> >bug in Eclispe?
> Your finally block might contain a return statement. If this is the
> case, the finally block doesn't complete normally. Normally means that
> you exit the block at the end of the block without going through a
> return or a throw statement.
> Paste one of your finally block and we can tell you for sure what is
> wrong.
> Because a finally block is executed even if there is an uncaught
> exception in the try block, a finally block that doesn't complete
> normally is a potential problem or source of bugs.
> For example, if you have something like this:

> class Example {
> static void foo() {
> throw new RuntimeException();
> }

> public static void main(String[] args) {
> try {
> foo();
> } finally {
> return;
> }
> }
> }

> Do you think that the exception will be thrown after the finally block
> is executed? The answer is no. Because you have a return statement,
> the runtime exception is "eaten".

> That example will throw the runtime exception:

> class Example {
> static void foo() {
> throw new RuntimeException();
> }

> public static void main(String[] args) {
> try {
> foo();
> } finally {
> }
> }
> }

> This is why we have such warning. Note this is only a warning. There
> is nothing wrong doing that. It could however hide a bug.

> Hope this help,
> --
> Olivier
Re: finally block does not complete normally [message #170083 is a reply to message #170077] Mon, 26 July 2004 16:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Mon, 26 Jul 2004 20:17:42 +0000 (UTC), shelli.orton@wmode.com
(Shelli) a écrit :
>Aha!
>Yes, all the finally blocks that are getting the warning have the return
>statement in them. I'll have to take a closer look and make sure there's
>no potential for hidden bugs and make changes as necessary.
The return statement prevents any unchecked exception from being
rethrown after the code in the finally block has been executed. I
would say this is a bad practice as it can hide any runtime exception
like NullPointerException :-).

If you are sure that you want to return once the finally is executed,
then simply put the return statement immediately after the
tr/catch/finally construct and not inside the finally block.
--
Olivier
Re: finally block does not complete normally [message #170255 is a reply to message #170083] Tue, 27 July 2004 07:45 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: bob.news.gmx.net

"Olivier Thomann" <olivier_thomannNO@ca.ibm.comSPAM> schrieb im
Newsbeitrag news:8gqag01eelsuotat0ff94djlg1u9bml28p@4ax.com...
> Le Mon, 26 Jul 2004 20:17:42 +0000 (UTC), shelli.orton@wmode.com
> (Shelli) a écrit :
> >Aha!
> >Yes, all the finally blocks that are getting the warning have the
return
> >statement in them. I'll have to take a closer look and make sure
there's
> >no potential for hidden bugs and make changes as necessary.
> The return statement prevents any unchecked exception from being
> rethrown after the code in the finally block has been executed. I
> would say this is a bad practice as it can hide any runtime exception
> like NullPointerException :-).
>
> If you are sure that you want to return once the finally is executed,
> then simply put the return statement immediately after the
> tr/catch/finally construct and not inside the finally block.

I prefer to put the return into the try block. Why? Because you then get
soon warned that you forgot to handle exceptions properly if you do:

// init
try {
// do stuff
return 1;
}
catch ( Exception ex ) {
// unhandled yet
}
finally {
// cleanup
}

This will raise a compiler error. This will not:

// init
try {
// do stuff
}
catch ( Exception ex ) {
// unhandled yet
}
finally {
// cleanup
}
// accidentally return the value that
// was intended to be the "ok" value:
return 1;


The finally block is executed in both cases.

Regards

robert
Re: finally block does not complete normally [message #170278 is a reply to message #170255] Tue, 27 July 2004 10:28 Go to previous message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Tue, 27 Jul 2004 13:45:07 +0200, "Robert Klemme" <bob.news@gmx.net>
a écrit :
>I prefer to put the return into the try block. Why? Because you then get
>soon warned that you forgot to handle exceptions properly if you do:
Sure. My point was simply to say that a return statement in a finally
block might have undesired side effects :-).
--
Olivier
Previous Topic:Where does the PackageExplorerPart add basic actions
Next Topic:Builders do not follow displayed order
Goto Forum:
  


Current Time: Wed May 07 20:29:41 EDT 2025

Powered by FUDForum. Page generated in 0.04219 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top