Java Reference
In-Depth Information
public static int readSafe(BufferedReader buffer, char[] cbuf,
int off, int len) throws IOEx-
ception {
int read = buffer.read(cbuf, off, len);
if (read == -1) {
throw new EOFException();
} else {
return read;
}
}
// ...
BufferedReader buffRdr;
// Set up buffRdr
try {
read = readSafe(buffRdr, chBuff, 0, MAX_READ);
chBuff[read] = TERMINATOR;
} catch (EOFException eof) {
chBuff[0] = TERMINATOR;
}
Applicability
Using in-band error indicators may result in programmers either failing to check status
codes or using incorrect return values, leading to unexpected behavior.
Given the comparatively rare occurrence of in-band error indicators in Java, it may be
possible to compile a list of all standard library methods that use them and to automat-
ically detect their use. However, detecting the safe use of in-band error indicators is not
feasible in the general case.
Returning an object that might be null on failure or a valid object on success is a
common example of an in-band error indicator. Although better method designs are of-
ten available, returning an object that may be null can be acceptable under some circum-
stances. See Guideline 26 , Always provide feedback about the resulting value of a meth-
od , ” for an example.
Bibliography
[API 2013]
Class Reader
Search WWH ::




Custom Search