Re: Holy boop: goto for Java
On 6/4/12 10:45 AM, Lew wrote:
Gene Wirchenko wrote:
Robert Klemme wrote:
Mike Schilling wrote:
* Do-while. It seems almost never to be what's needed. Though I do quite
often need
while(true)
{
stuff
if (condition)
{
break;
}
more stuff
}
Interesting: I cannot remember having needed this. While I do remember
using do {} while. What use cases do you have for the construct above?
In the sense that there are other ways one can do this, no, you
do not need this. It can be useful if you have multiple tests in that
loop, especially multiple tests that cannot be combined. An example
of this would be a body of:
process first piece
if error
break or continue as needed
process middle piece
if error
break or continue as needed
process last piece
[snip]
public void processResources(String ... resourceNames)
{
for (String name : resourceNames)
{
BufferedReader br;
try
{
br = new BufferedReader(new FileReader(name));
FWIW, this is a potential resource leak (think OOM on the "new
BufferedReader")
}
catch(FileNotFoundException exc)
{
logger.error("Cannot find "+ name, exc);
continue;
}
assert br != null; // and is valid
br is never assigned null, you could make br final. I think it *should*
be final.
try
{
doSomething(br);
}
finally
{
try
{
br.close();
}
catch(IOException exc)
{
logger.error("Cannot close "+ name, exc);
continue;
}
}
reportComplete(name);
}
}
(Not using try-with-resources here, in order to illustrate the idiom.)
Hmm, a failure to close causes a resource processing not to be complete?