Next: , Previous: , Up: baselib   [Index]


4.13 Errors and violations

Procedure: error who message irritant1
Procedure: assertion-violation who message irritant1

who must be a string or a symbol or #f. message must be a string. The irritants are arbitrary objects.

These procedures raise an exception. The error procedure should be called when an error has occurred, typically caused by something that has gone wrong in the interaction of the program with the external world or the user. The assertion-violation procedure should be called when an invalid call to a procedure was made, either passing an invalid number of arguments, or passing an argument that it is not specified to handle.

The who argument should describe the procedure or operation that detected the exception. The message argument should describe the exceptional situation. The irritants should be the arguments to the operation that detected the operation.

The condition object provided with the exception has the following condition types:

Moreover, the condition created by error has condition type &error, and the condition created by assertion-violation has condition type &assertion.

(define (fac n)
  (if (not (integer-valued? n))
      (assertion-violation
       'fac "non-integral argument" n))
  (if (negative? n)
      (assertion-violation
       'fac "negative argument" n))
  (letrec
    ((loop (lambda (n r)
             (if (zero? n)
                 r
                 (loop (- n 1) (* r n))))))
      (loop n 1)))

(fac 5)         ⇒ 120
(fac 4.5)       ⇒ exception &assertion
(fac -3)        ⇒ exception &assertion
Syntax: assert ?expression

An assert form is evaluated by evaluating ?expression. If ?expression returns a true value, that value is returned from the assert expression. If ?expression returns #f, an exception with condition types &assertion and &message is raised. The message provided in the condition object is implementation–dependent.

NOTE Implementations should exploit the fact that assert is a syntax to provide as much information as possible about the location of the assertion failure.


Next: , Previous: , Up: baselib   [Index]