Within Carapace, all error handling is done by throwing and catching errors. If a function or method wishes to raise an error, it constructs an Error object and then throws it. The script evaluation then unwinds until it gets to a point where this particular type of error is caught.
The common alternative for error handling is for functions to return an error and for the caller to deal with this in the correct way. You can of course do this with Carapace, since you can return an Error object in the same way as you can return any other object from a function. The advantage of the throw/catch style is that the caller does not need to do the error checking -- the called function either is successful and returns the appropriate value, or it is throws an error. In this error case, the caller never sees the return of the function.
Wherever an error is caught, Carapace requires there is a local
variable to be
in existence called err of type Error. This
variable gets instantiated with the caught error.
The error-handling functions are as follows:
| try | show the intention to catch errors generated in the following expression |
| catch | catch a given class of errors |
| throw | throw an error - so it can be caught elsewhere |
The example shows the use of try, throw
and catch.
try
Show the intention to catch errors generated in the following expression. The
example shows the use of try, throw
and catch.
Arguments:
| expression | List |
| ... | one or more catch-expressions |
Return type: the empty List () is returned.
catch
Catch a given class of errors. Each Error object has an
Integer class. A catch-statement either
catches just a single class (eg. class 5) or all errors as follows:
(catch -1
(printAux "The error caught was " err.string)
)
Arguments:
| errorClass | Integer |
| ... | sequence of List expressions
to evaluate
|
Return type: the empty List () is returned.
throwThrow an error - so it can be caught elsewhere. The effect of throwing an error is to unwind the call stack back to the place where the error is caught. The effect is to jump out of the current script location to the first appropriate catch statement which may well be in a different function.
Arguments:
| err | Error |
Return type: the empty List () is returned.
| Contents | Index | Current topic: functions | Related topics: objects |