Chapter 32: a catch that names its type, built

A catch names the type it handles, and only that type - and what it does not name goes on to the next enclosing try. Here charge throws a plain Exception for a negative amount and a CardDeclined for zero; the inner catch takes only the second, so the first escalates to the outer one. The IDE writes the class (as open, extending Exception), its default isSet operator, charge's signature and the program's shell; you type the constructor that hands its reason to Exception, the two throws and the two tries. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-narrow-catch.

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

  defines class

    CardDeclined extends Exception as open
      CardDeclined()
        -> reason as String
        super(reason)

      default operator ?

  defines function

    charge()
      -> amount as Integer
      <- rtn as Integer: amount
      if amount < 0
        throw Exception("amount must not be negative")
      if amount == 0
        throw CardDeclined("nothing to charge")

  defines program

    NarrowCatch()
      stdout <- Stdout()
      try
        for amount in [0, -1, 5]
          try
            stdout.println(`charged ${charge(amount)}`)
          catch
            -> ex as CardDeclined
            stdout.println(`declined here: ${ex.reason()}`)
      catch
        -> ex as Exception
        stdout.println(`escalated: ${ex.reason()}`)

Step 1: CardDeclined — an exception of our own

Choose Code > New Construct > Class…

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

CardDeclined is the class's name. Choose Open under Extensibility - a class is closed to extension unless it says so. Type Exception into Extends. Choose Preview.

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

The preview shows CardDeclined about to be written - and, if Exception owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows CardDeclined about to be written - and, if Exception owes any methods, an override stub for each

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

Right-click the CardDeclined line and choose Add Operator > ? (isSet) > default - the compiler writes the operator from the fields.

The EK9 IDE once this is done: Right-click the CardDeclined line and choose Add Operator > ? (isSet) > default - the compiler writes the operator from the fields

Below the class line, type the constructor: a reason in, handed to Exception's own constructor with super(...). No tool writes a constructor whose parameter is not a field. Saves when typed.

The EK9 IDE once this is done: Below the class line, type the constructor

Step 2: charge — it throws two ways

Choose Code > New Construct > Function…

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

charge is the function's name. Add one parameter, amount of type Integer, set Returns to Integer. Choose Preview.

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

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

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

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

Select the stub, from <- rtn as Integer? down to its //AI line, and type charge'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 Integer? down to its //AI line, and type charge'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…

NarrowCatch is the program's name. Choose Preview.

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

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

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

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

declined here: nothing to charge, then escalated: amount must not be negative - the 5 never ran. Choose Close.

The EK9 IDE just before choosing Close: declined here

The result

NarrowCatch.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. The CardDeclined for 0 was caught where it happened, the loop went on, and the plain Exception for -1 went past the inner catch to the outer one and ended the loop.

The EK9 IDE when the lesson has finished

Where this lesson comes from