10.1 Exceptions
Whenever a run-time error occurs, an exception is raised. Unless the exception is caught, then it is handled by printing a message associated with the exception, and then escaping from the computation.
> (/ 1 0) |
/: division by zero |
> (car 17) |
car: expects argument of type <pair>; given 17 |
To catch an exception, use the with-handlers form:
(with-handlers ([predicate-expr handler-expr] )
body )
Each predicate-expr in a handler determines a kind of exception that is caught by the with-handlers form, and the value representing the exception is passed to the handler procedure produced by handler-expr. The result of the handler-expr is the result of the with-handlers expression.
For example, a divide-by-zero error raises an instance of the exn:fail:contract:divide-by-zero structure type:
| |||
+inf.0 | |||
| |||
car: expects argument of type <pair>; given 17 |
The error function is one way to raise your own exception. It packages an error message and other information into an exn:fail structure:
> (error "crash!") | ||
crash! | ||
| ||
air-bag |
The exn:fail:contract:divide-by-zero and exn:fail structure types are sub-types of the exn structure type. Exceptions raised by core forms and functions always raise an instance of exn or one of its sub-types, but an exception does not have to be represented by a structure. The raise function lets you raise any value as an exception:
> (raise 2) | ||
uncaught exception: 2 | ||
| ||
two | ||
| ||
/: division by zero |
Multiple predicate-exprs in a with-handlers form let you handle different kinds of exceptions in different ways. The predicates are tried in order, and if none of them match, then the exception is propagated to enclosing contexts.
| ||||
> (always-fail 2) | ||||
even | ||||
> (always-fail 3) | ||||
positive | ||||
> (always-fail -3) | ||||
uncaught exception: -3 | ||||
| ||||
negative |
Using (lambda (v) #t) as a predicate captures all exceptions, of course:
| ||
oops |
Capturing all exceptions is usually a bad idea, however. If the user types Ctl-C in a terminal window or clicks the Stop button in DrScheme to interrupt a computation, then normally the exn:break exception should not be caught. To catch only exceptions that represent errors, use exn:fail? as the predicate:
| |||
oops | |||
| |||
user break |