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.
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…
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 preview shows firstLabel about to be written. Choose Apply.
Step 2: Constants
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 preview shows secondLabel about to be written. Choose Apply.
Step 3: Constants
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 preview shows brokenLabel about to be written. Choose Apply.
Step 4: Connection — a closeable that can fail to open
Choose Code > New Construct > Class…
Connection is the class's name. Choose Preview.
The preview shows Connection about to be written. Choose Apply.
Select the Connection line and, above it, type the blank line the book opens its first class with. Saves when typed.
Right-click the Connection line and choose Add Field…
Type name <- String() - the field and the value it starts with. Choose OK.
The preview shows name added - and default operator ? with it, Connection's first field. Choose Apply.
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.
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.
Select the stub and type the close. Saves when typed.
Step 5: The program
Choose Code > New Construct > Program…
ResourceOrder is the program's name. Choose Preview.
The preview shows ResourceOrder about to be written. Choose Apply.
Select the ResourceOrder() line and, above it, type the blank line the book opens the program with. Saves when typed.
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.
Step 6: Run it
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 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.
Where this lesson comes from
- Answers -
ek9 -qand the IDE's Answers tab: 137. - For AI assistants - the MCP tool
ek9_new_construct,ek9_add_field,ek9_add_constructor,ek9_add_operator,ek9_add_program.





























