Chapter 32: an exception that ends the program, built
An Exception nothing catches ends the program, and its exit code is the program's exit status - Exception takes an optional second argument for it, here 3. The IDE writes loadSettings' signature and the program's shell; you type the throw, and the try with only a finally. The run is MEANT to end with that exception. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-fatal-exit.
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.exit.fatal defines function loadSettings() -> fileName as String <- rtn as String? if not fileName? throw Exception("No settings file named", 3) rtn: `Loaded ${fileName}` defines program FatalExit() stdout <- Stdout() stdout.println(loadSettings("app.properties")) try stdout.println("Loading settings with no file name") stdout.println(loadSettings(String())) finally stdout.println("Cleanup ran before the process ended")
Step 1: loadSettings — a function
Choose Code > New Construct > Function…
loadSettings is the function's name. Add one parameter, fileName of type String, set Returns to String. Choose Preview.
The preview shows loadSettings about to be written. Choose Apply.
Select the loadSettings() line and, above it, type the blank line the book opens its first function with. Saves when typed.
Select the stub, from <- rtn as String? down to its //AI line, and type loadSettings's own return and work, as the book writes them. Saves when typed.
Step 2: The program
Choose Code > New Construct > Program…
FatalExit is the program's name. Choose Preview.
The preview shows FatalExit about to be written. Choose Apply.
Select the FatalExit() 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 FatalExit's body exactly as the chapter shows it. Saves when typed.
Step 3: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
Loaded app.properties, Loading settings with no file name, Cleanup ran before the process ended - and then the uncaught Exception: No settings file named, Exit Code: 3. Choose Close.
The result
FatalExit.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. The finally ran, then the process ended with the exception's own exit code, 3 - an exception with no code would have ended it with 1.
Where this lesson comes from
- Answers -
ek9 -qand the IDE's Answers tab: 136, 1431. - For AI assistants - the MCP tool
ek9_new_construct,ek9_add_program.













