JULIA / CHAPTER 10

Missing data and recoverable failures

Advanced

Keep absence, unknown measurements, and exceptions distinct.

By the end of this chapter

  • Distinguish nothing from missing
  • Use tryparse for expected invalid text
  • Handle failures at a useful boundary

The idea

nothing commonly represents the absence of a useful result, while missing represents an unknown data value. They serve different contracts and should not be substituted casually. tryparse returns a parsed value or nothing when text is invalid, making ordinary validation possible without throwing an exception for every rejected input. Test the result explicitly before using it in arithmetic.

Many operations propagate missing so uncertainty remains visible. skipmissing provides an iterator over the known values, and coalesce chooses a fallback for missing values. State the policy for an entirely missing collection rather than assuming that any numeric summary is meaningful. Exceptions signal failures that interrupt normal execution; use try and catch where the program can recover, and avoid hiding unrelated errors with a broad fallback that makes corrupted results look valid.

Julia example
function next_count(text)
    value = tryparse(Int, text)
    value === nothing && return "invalid"
    string(value + 1)
end
println(next_count("8"))
println(next_count("eight"))
println(sum(skipmissing([2, missing, 5])))
Result
9
invalid
7

Valid text produces a number before incrementing. Invalid text takes an explicit absence branch, and the final summary deliberately adds only the two known measurements.

Try it yourself

Parse a nonnegative integer from text, returning nothing for invalid or negative input and the number otherwise.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

What does tryparse(Int, "four") return?