Re: nihil
"Tom Hawtin" <usenet@tackline.plus.com> wrote in message
news:4616ea3d$0$8742$ed2619ec@ptn-nntp-reader02.plus.net...
Roedy Green wrote:
how would you code this:
catch ( java.io.IOException e )
{
// do nothing
}
Algol 68 had the nihil statement for explicitly saying you were doing
nothing. Java won't accept null by itself as a valid statement, though
it will accept function calls that evaluate to null.
Why not just a comment? Lint-like code inspectors think this is a
probable error.
I think it's still probably an error even if the comment is present.
That's my experience anyway.
Do you really want to do nothing if some code is screaming blue murder?
Normally the catch block should at the very least throw new Error(exc);.
There are circumstances when you simply don't care, for instance:
FileInputStream fis = new FileInputStream(fname);
try
{
. // read and process the first three lines of the file.
}
finally
{
try
{
fis.close();
}
catch (IOException ex)
{
}
}
If I can read and process what I'm interested in, it's of no interest to me
that the close fails.
That's true even if you "know" it wont actually happen (like using
ByteArrayInputStream).
I use RuntimeException for that, e.g.
String name;
try
{
name = new String(btyes, "UTF-8")
}
catch (UnsupportedEncodingException ex)
{
// Can't happen, since all Java installations support UTF-8
throw new RuntimeException(ex);
}
Is there a reason to prefer Error?