Chapter 32: try as an expression, built

In Java a try is a statement: to get a value out of it you declare a variable first and assign it in both arms. In EK9 try is an expression - it has a return of its own, and the catch supplies the value when the body throws. The chapter's TryExpression reads a port number that may not be one. The IDE writes the constant, the function's signature and the program's shell, with its parameter; you type what the function throws and the try itself. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-try-expression.

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.try.expression

  defines constant
    fallbackPort <- 8080

  defines function

    portFrom()
      -> text as String
      <- rtn as Integer: Integer(text)
      if not rtn?
        throw Exception(`not a port number: ${text}`)

  defines program

    TryExpression()
      -> configured as String
      stdout <- Stdout()

      port <- try
        <- rtn as Integer: portFrom(configured)
      catch
        -> ex as Exception
        stdout.println(`using the fallback: ${ex.reason()}`)
        rtn: fallbackPort

      stdout.println(`listening on ${port}`)

Step 1: A constant

Choose Code > New Construct > Constant…

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

fallbackPort is the constant's name and 8080 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: fallbackPort is the constant's name and 8080 its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: portFrom — a function

Choose Code > New Construct > Function…

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

portFrom is the function's name. Add one parameter, text of type String, set Returns to Integer. Choose Preview.

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

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

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

Select the portFrom() 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 portFrom() 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 portFrom'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 portFrom'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…

TryExpression is the program's name. Add one parameter, configured of type String. Choose Preview.

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

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

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

Select the TryExpression() 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 TryExpression() line and, above it, type the blank line the book opens the program with

Select the stub, from require configured? down to its //AI line, and type TryExpression's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require configured? down to its //AI line, and type TryExpression'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…

Type abc into Arguments (space-separated), then choose Run.

The EK9 IDE just before choosing Run: Type abc into Arguments (space-separated), then choose Run

Given abc, it prints using the fallback: not a port number: abc, then listening on 8080 - the catch's value became port. Choose Close.

The EK9 IDE just before choosing Close: Given abc, it prints using the fallback

The result

TryExpression.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it: port is the value of the try, either portFrom's answer or the fallback the catch assigned to rtn after reporting why.

The EK9 IDE when the lesson has finished

Where this lesson comes from