Chapter 32: when a resource closes, built

In Java the resources of a try-with-resources are closed BEFORE catch and finally run. In EK9 the body runs, then the try's own catch, then its finally, and only then are the resources closed, last opened first - so a catch runs while the resources are still open. If you write from Java intuition you will have this backwards. This program shows three cases: the body completes, the body throws, and the second resource cannot be opened. The IDE writes the constants, the Connection's shape and the program's shell; you type the constructor that can throw, the close and the three tries. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-resource-order.

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.resources.order

  defines constant
    firstLabel <- "first"

    secondLabel <- "second"

    brokenLabel <- "broken"

  defines class

    Connection
      name <- String()

      Connection()
        -> label as String
        if label == brokenLabel
          throw Exception(`cannot open ${label}`)
        name := label
        Stdout().println(`opened ${name}`)

      operator close as pure
        Stdout().println(`closed ${name}`)

      default operator ?

  defines program

    ResourceOrder()
      stdout <- Stdout()

      stdout.println("--- the body completes, with catch and finally ---")
      try
        ->
          first <- Connection(firstLabel)
          second <- Connection(secondLabel)
        stdout.println("body done")
      catch
        -> ex as Exception
        stdout.println(`catch: ${ex.reason()}`)
      finally
        stdout.println("finally")

      stdout.println("--- the body throws, with catch and finally ---")
      try
        ->
          first <- Connection(firstLabel)
          second <- Connection(secondLabel)
        throw Exception("body failed")
      catch
        -> ex as Exception
        stdout.println(`catch: ${ex.reason()}`)
      finally
        stdout.println("finally")

      stdout.println("--- the second resource cannot be opened ---")
      try
        try
          ->
            first <- Connection(firstLabel)
            second <- Connection(brokenLabel)
          stdout.println("never reached")
        catch
          -> ex as Exception
          stdout.println(`inner catch: ${ex.reason()}`)
      catch
        -> ex as Exception
        stdout.println(`outer catch: ${ex.reason()}`)

Step 1: Constants

Choose Code > New Construct > Constant…

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

firstLabel is the constant's name and "first" its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: firstLabel is the constant's name and "first" its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: Constants

Choose Code > New Construct > Constant…

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

secondLabel is the constant's name and "second" its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: secondLabel is the constant's name and "second" its value - a literal in the Value field, from which the type is inferred

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

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

Step 3: Constants

Choose Code > New Construct > Constant…

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

brokenLabel is the constant's name and "broken" its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: brokenLabel is the constant's name and "broken" its value - a literal in the Value field, from which the type is inferred

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

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

Step 4: Connection — a closeable that can fail to open

Choose Code > New Construct > Class…

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

Connection is the class's name. Choose Preview.

The EK9 IDE just before choosing Preview: Connection is the class's name

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

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

Select the Connection line and, above it, type the blank line the book opens its first class with. Saves when typed.

The EK9 IDE once this is done: Select the Connection line and, above it, type the blank line the book opens its first class with

Right-click the Connection line and choose Add Field…

The EK9 IDE once this is done: Right-click the Connection line and choose Add Field…

Type name <- String() - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type name <- String() - the field and the value it starts with

The preview shows name added - and default operator ? with it, Connection's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows name added - and default operator ? with it, Connection's first field

Right-click the Connection line and choose Add Constructor > From All Fields - a constructor that takes every field.

The EK9 IDE once this is done: Right-click the Connection line and choose Add Constructor > From All Fields - a constructor that takes every field

Select the constructor and type the book's: a label in - and a broken one throws before anything is opened. Saves when typed.

The EK9 IDE once this is done: Select the constructor and type the book's

Right-click the Connection line and choose Add Operator > Custom Operator…

The EK9 IDE once this is done: Right-click the Connection line and choose Add Operator > Custom Operator…

close is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field. Choose OK.

The EK9 IDE just before choosing OK: close is not one of the eight operators with their own menu entry - type it here, in Custom Operator…'s one field

Select the stub and type the close. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the close

Step 5: The program

Choose Code > New Construct > Program…

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

ResourceOrder is the program's name. Choose Preview.

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

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

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

Select the ResourceOrder() 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 ResourceOrder() 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 ResourceOrder'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 ResourceOrder's body exactly as the chapter shows it

Step 6: 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.

Three blocks: opened first, opened second, body done, finally, closed second, closed first; the same with catch: body failed; and finally opened first, closed first, outer catch: cannot open broken. Choose Close.

The EK9 IDE just before choosing Close: Three blocks

The result

ResourceOrder.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. In the first two runs 'finally' comes before 'closed second' and 'closed first'. In the third, a failure while ACQUIRING a resource is not handled by the try that declares it: the inner catch printed nothing, first was closed, and the outer try caught it.

The EK9 IDE when the lesson has finished

Where this lesson comes from