Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

An in-band error indicator is a type returned by a function that can either indicate a either a legitimate return value, or an illegitimate value that indicates an error of some sort. Some common examples of in-band error indicators include:

  • A valid object or null
  • An integer indicating a positive value, or -1 to indicate that an error occurred
  • An array of valid objects or null indicating no the absence of valid objects

In-band-error indicators require checking for the error; however this checking is often overlooked. In addition to violating Failure to check for such error conditions not only violates EXP00-J. Do not ignore values returned by methods, this but also has the unfortunate effect of propagating invalid values being that may subsequently be treated as valid in future later computations.

Avoid the use of in-band error indicators. They are much less common in Java than in some other programming languages; nevertheless, but they are used in the read(byte[] b, int off, int len) and read(char[] cbuf, int off, int len) families of methods in java.io.

...

This noncompliant code example attempts to read into an array of characters and to add an extra character into the buffer immediately after the characters read:

...

However, if the input buffer is initially at end-of-file, then the read method will return −1, and the attempt to place the terminator character will throw an ArrayIndexOutOfBoundsException.

...