So you mean that catch always returns the value of :
If there is no throw:
the value of the function evaluated
If there is a throw:
the value throwed by such a call.
I supposed it returned nil in the second case.
So If I need to differentiate the two cases I must follow one of the following strategies :
a) always return <b>nil</b> from a successfull execution of a function
and throw a meaningful error code in case of error.
b) always return something different from nil from a successfull execution of a function, and throw nil in case of error
I suppose strategy A is the best.
In general (in other languages) I'm accustomed to the use of catch/throw when I have to check user input.
Generally my pattern is the following (pseudo-code) :
Code: Select all
(define (do-checks)
(set 'value1 (get-a-number-from-input-form 'field1))
(set 'value2 (get-a-number-from-input-form 'field2))
(if (< value2 value1) (throw (list "value 2 must be greater or equal to value1" 'field2)))
(set 'value3 (get-a-number-from-input-form 'field3))
(set 'value4 (get-a-date-from-input-form 'field4))
---
(save-data-somewhere value1 value2 value3 value4))
(if (catch (do-checks) error)
(show-the-error-to-user error))
The various get-a-xxx-from-input-form checks the formal validity
of the fields, perhaps calling subprocedures that can throw errors,
and they throws an error if something is invalid, with an indication
to what field was wrong, in order to set the cursor, if possible to such a field.
Additional checks are made in the do-checks routine.
To use such a pattern I need to differentiate the outcome of the
catch function.
Thanks for your answer,
Maurizio.