Chapter 32: exceptions inside a pipeline, built

A pipeline stage is an ordinary function, so an exception thrown in one travels as any other does. Two things can happen to it: a try around the whole pipeline statement catches it and nothing is collected, or a stage catches inside itself and substitutes a value, and the pipeline carries on. The IDE writes both functions' signatures and the program's shell; you type the throw, the stage's own try and the two pipelines. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-pipeline-failures.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.pipeline.failures

  defines function

    parsePrice()
      -> text as String
      <- rtn as Integer: Integer(text)
      if not rtn?
        throw Exception(`not a price: ${text}`)

    parsePriceOrZero()
      -> text as String
      <- rtn as Integer: 0
      try
        rtn: parsePrice(text)
      catch
        -> ex as Exception
        Stdout().println(`skipped: ${ex.reason()}`)

  defines program

    PipelineFailures()
      stdout <- Stdout()
      prices <- ["12", "oops", "30", "45"]

      try
        total <- cat prices | map with parsePrice | collect as List of Integer
        stdout.println(`unreachable: ${total}`)
      catch
        -> ex as Exception
        stdout.println(`pipeline stopped: ${ex.reason()}`)

      recovered <- cat prices | map with parsePriceOrZero | collect as List of Integer
      stdout.println(`recovered: ${recovered}`)

Step 1: parsePrice — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

parsePrice is the function's name. Add one parameter, text of type String, set Returns to Integer. Choose Preview.

The EK9 IDE just before choosing Preview: parsePrice is the function's name

The preview shows parsePrice about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows parsePrice about to be written

Select the parsePrice() line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the parsePrice() line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as Integer? down to its //AI line, and type parsePrice's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Integer? down to its //AI line, and type parsePrice's own return and work, as the book writes them

Step 2: parsePriceOrZero — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

parsePriceOrZero is the function's name. Add one parameter, text of type String, set Returns to Integer. Choose Preview.

The EK9 IDE just before choosing Preview: parsePriceOrZero is the function's name

The preview shows parsePriceOrZero about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows parsePriceOrZero about to be written

Select the stub, from <- rtn as Integer? down to its //AI line, and type parsePriceOrZero's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Integer? down to its //AI line, and type parsePriceOrZero's own return and work, as the book writes them

Step 3: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

PipelineFailures is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: PipelineFailures is the program's name

The preview shows PipelineFailures about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows PipelineFailures about to be written

Select the PipelineFailures() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the PipelineFailures() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type PipelineFailures's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type PipelineFailures's body exactly as the chapter shows it

Step 4: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

pipeline stopped: not a price: oops, then skipped: not a price: oops, then recovered: [12, 0, 30, 45]. Choose Close.

The EK9 IDE just before choosing Close: pipeline stopped

The result

PipelineFailures.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. The first pipeline stopped at oops, which is why unreachable never printed; the second recovered, with 0 standing in for the price that would not parse.

The EK9 IDE when the lesson has finished

Where this lesson comes from