This is the documentation for stable 1.21. You can switch to the 2.0 specification at the top.

Read and Handle Errors

Find where to fix a source error and use guard to handle an error during execution.

The house style rule from the previous lesson stops with an error when the line count is not a number. In this lesson you read that diagnostic and wrap the rule with guard so it returns a set decision instead. You also fix an error that Lispex reports before a program runs.

Read the previous lesson, First Decision Rule, first. The file examples below use the Native lispex command. Install it from Downloads.

When the rule gets the wrong input

Save this complete program as house-style.lspx. It calls the rule from the previous lesson with the string "two" as the line count.

LISPEX
(define (heading-placement lines-left chapter-heading?)
  (if (and (>= lines-left 2) (not chapter-heading?))
      '(decision keep)
      '(decision next-page)))

(heading-placement "two" #f)
BASH
lispex run house-style.lspx

The command writes this diagnostic to standard error and exits with status 1. Standard output is empty.

E312 house-style.lspx:2:12 >=: expected a number, got "two"

Read the line from left to right. E312 is the code. house-style.lspx:2:12 names the file, line 2, and column 12, where (>= lines-left 2) starts. The message says that >= expected a number and received the string "two". Error and Warning Codes lists every code.

A misspelled name gives a different code at the position of that name. If the final call misspells heading-placement as heading-placment, the diagnostic is E300 house-style.lspx:6:2 unbound variable: heading-placment.

Return a decision for wrong input

The error happens while the rule runs, so guard can catch it. Replace the final call with a procedure that wraps the rule, and call it twice.

LISPEX
(define (heading-placement lines-left chapter-heading?)
  (if (and (>= lines-left 2) (not chapter-heading?))
      '(decision keep)
      '(decision next-page)))

(define (checked-placement lines-left chapter-heading?)
  (guard (e ((error-object? e) '(decision review)))
    (heading-placement lines-left chapter-heading?)))

(list (checked-placement 2 #f)
      (checked-placement "two" #f))

Run lispex run house-style.lspx again.

((decision keep) (decision review))

The command exits with status 0, leaves standard error empty, and creates no files. A valid line count still gets its usual decision. For "two", >= raises an error object, guard catches it, and the rule returns (decision review). The application decides what to do with a review decision.

Fix E100 before running the program

Save this complete program as syntax.lspx. The closing parenthesis is missing.

LISPEX
(+ 1 2

Run it.

BASH
lispex run syntax.lspx

The command writes this diagnostic to standard error and exits with status 1. Standard output is empty.

E100 syntax.lspx:1:1 unterminated list (missing `)`)

syntax.lspx:1:1 points to line 1, column 1, where the unfinished list starts. Add the closing parenthesis.

LISPEX
(+ 1 2)

Run the same command again.

BASH
lispex run syntax.lspx
3

The command now writes 3 to standard output and exits with status 0. Neither run creates an output file.

E100 occurs while Lispex reads the source, before guard can run. Repair the source first.

Handle an error during execution

Save this complete program as user-error.lspx.

LISPEX
(guard (e ((error-object? e) (error-object-message e))) (error "bad input"))
BASH
lispex run user-error.lspx
"bad input"

The command exits with status 0, leaves standard error empty, and creates no output file.

  1. error creates an error object containing the message "bad input".
  2. guard catches that object and binds it to e.
  3. error-object? checks the value. The matching clause returns its message.

The whole guard expression returns the string. It does not resume at the failing error call. Inspect error objects with error-object?, error-object-message, and error-object-irritants instead of parsing printed diagnostics.

Decide whether to repair or handle

What happenedWhat to do
Reading or checking a form failed before executionFix the indicated source. A handler has not started yet.
A catchable runtime error occurredFix the operation or use guard to return an appropriate result.
No guard clause matched and there is no elseThe original raised object is raised again for an outer handler.
A resource limit was reachedReduce the work or review the resource profile. guard cannot catch ResourceLimit.
The calling application crashedInvestigate the application failure. It is not converted into a Lispex error object.

Continue from a raised condition

Use with-exception-handler when you need a handler procedure. raise-continuable lets that procedure return a value to the call that raised the condition.

Paste this complete example into the Playground.

LISPEX
(with-exception-handler
  (lambda (e) 10)
  (lambda () (+ 1 (raise-continuable "retry"))))

Result

OUTPUT
11

The handler returns 10, then + adds 1. With raise instead, returning normally from the handler produces E332. Use guard when you want to leave the failing expression and return from the surrounding guard.

Exercise

Predict what (checked-placement 2 "no") returns. The chapter-heading flag is a string instead of a boolean.

Show answer

It returns (decision next-page) without an error. not accepts any value, and only #f is false, so the string counts as true. guard catches only errors. A value of the wrong type can still produce an answer, so the application checks its inputs before calling the rule.

Continue to Recursion and Tail Calls to write a procedure that sums a list.

Read Errors and Warnings · Error and Warning Codes · Run a Decision Rule with JSON Input