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.

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

The EK9 IDE once this is done: 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 EK9 IDE just before choosing Preview: loadSettings is the function's name

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

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

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

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.

The EK9 IDE once this is done: 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

Step 2: The program

Choose Code > New Construct > Program…

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

FatalExit is the program's name. Choose Preview.

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

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

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

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

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

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 EK9 IDE just before choosing Close: Loaded app.properties, Loading settings with no file name, Cleanup ran before the process ended - and then the uncaught Exception

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.

The EK9 IDE when the lesson has finished

Where this lesson comes from