Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed redundant CS, extended intro

An in-band error indicator is a type returned by a function that can either indicate 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 valud, or -1 to indicate that an error occurred
  • An array of valid objects or null indicating no valid objects

In-band-error indicators require checking for the error; however this checking is often overlooked. This has the unfortunate effect of invalid values being treated as valid in future computations.

Avoid the use of in-band error indicators. They are much less common in Java than in some other programming languages, 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.

...

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.

Compliant Solution (Extending)

This compliant solution defines a SafeBufferedReader class and introduces a readSafe method that throws an exception if end-of-file is detected:

...

bgColor#ccccff

...

Compliant Solution (Wrapping)

This compliant solution defines a readSafe method that wraps the original read method and throws an exception if end-of-file is detected:

...