Chapter 32: a close that fails, built

A close can throw. That failure leaves the try after the try's own catch has run, so a catch on the same try does not see it: it goes to the enclosing try. And if the body had already failed, the close failure does not replace the first exception - it is attached to it, and read through suppressed(), as in Java. The inner try has no catch on purpose. The IDE writes the Journal's shape, the close's shape, report's signature and the program's shell; you type the close that throws and the two tries. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-suppressed-close.

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.suppressed

  defines class

    Journal
      name <- String()

      Journal()
        -> label as String
        name := label

      post()
        -> entry as String
        Stdout().println(`${name} posts ${entry}`)

      operator close as pure
        throw Exception(`close of ${name} failed`)

      default operator ?

  defines function

    report()
      -> ex as Exception
      stdout <- Stdout()
      stdout.println(`caught: ${ex.reason()}`)
      for failure in ex.suppressed()
        stdout.println(`  suppressed: ${failure.reason()}`)

  defines program

    SuppressedClose()
      stdout <- Stdout()

      stdout.println("--- the body completes, the close fails ---")
      try
        try
          -> journal <- Journal("audit")
          journal.post("debit 100")
      catch
        -> ex as Exception
        report(ex)

      stdout.println("--- the body fails, then the close fails ---")
      try
        try
          -> journal <- Journal("audit")
          journal.post("debit 100")
          throw Exception("posting failed")
      catch
        -> ex as Exception
        report(ex)

Step 1: Journal — a closeable whose close fails

Choose Code > New Construct > Class…

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

Journal is the class's name. Choose Preview.

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

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

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

Select the Journal 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 Journal line and, above it, type the blank line the book opens its first class with

Right-click the Journal line and choose Add Field…

The EK9 IDE once this is done: Right-click the Journal 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, Journal's first field. Choose Apply.

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

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

The EK9 IDE once this is done: Right-click the Journal 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, name set from it. Saves when typed.

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

Right-click the Journal line and choose Add Method…

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

post is the method's name. Add one parameter, entry of type String. Choose Apply.

The EK9 IDE just before choosing Apply: post is the method's name

Select the stub and type post's work. Saves when typed.

The EK9 IDE once this is done: Select the stub and type post's work

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

The EK9 IDE once this is done: Right-click the Journal 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 a close that throws. Saves when typed.

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

Step 2: report — what a handler sees

Choose Code > New Construct > Function…

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

report is the function's name. Add one parameter, ex of type Exception. Choose Preview.

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

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

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

Select the report() 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 report() line and, above it, type the blank line the book opens its first function with

Select the stub, from require ex? down to its //AI line, and type report's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require ex? down to its //AI line, and type report'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…

SuppressedClose is the program's name. Choose Preview.

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

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

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

Select the SuppressedClose() 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 SuppressedClose() 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 SuppressedClose'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 SuppressedClose'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.

The first case ends caught: close of audit failed; the second, caught: posting failed with the close failure listed under suppressed. Choose Close.

The EK9 IDE just before choosing Close: The first case ends caught

The result

SuppressedClose.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. In the second case the handler sees the body's own exception, posting failed, and the close failure is in its suppressed() list.

The EK9 IDE when the lesson has finished

Where this lesson comes from