Reading the close failures suppressed onto an Exception

The complete source of exceptions_suppressed.ek9, compiled by the EK9 compiler. Shown on exceptions.html.

#!ek9
defines module site.exceptions.suppressed

  defines class

    Part
      name as String?

      default private Part()

      Part()
        -> label as String
        name: label

      operator close as pure
        stdout <- Stdout()
        stdout.println(`closing ${name}`)
        if name <> "totals"
          throw Exception(`close of ${name} failed`)

      default operator ?

    Ledger
      journal as Part: Part("journal")
      index as Part: Part("index")
      totals as Part: Part("totals")

      default operator close

      default operator ?

  defines function

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

    describe()
      ->
        failure as Exception
        indent as String
      Stdout().println(`${indent}suppressed: ${failure.reason()}`)
      for inner in failure.suppressed()
        describe(inner, indent + "  ")

  defines program

    InspectSuppressed()
      stdout <- Stdout()

      try
        try
          -> ledger <- Ledger()
          stdout.println("using the ledger")
      catch
        -> ex as Exception
        report(ex)

      try
        try
          -> ledger <- Ledger()
          require ledger?
          throw Exception("the posting failed")
      catch
        -> ex as Exception
        report(ex)

      try
        throw Exception("nothing to close")
      catch
        -> ex as Exception
        report(ex)
        stdout.println(`nothing suppressed: ${length ex.suppressed() == 0}`)

Download exceptions_suppressed.ek9

Output

using the ledger
closing totals
closing index
closing journal
caught: close of index failed
  suppressed: close of journal failed
closing totals
closing index
closing journal
caught: the posting failed
  suppressed: close of index failed
    suppressed: close of journal failed
caught: nothing to close
nothing suppressed: true