Leo: |
EX empties the stack, a 1 is entered and is DUPlicated.
[rtn] ( 1 1 0 ) okOh yes, and an extra zero. Why? |
Theo: |
I'll show you.
EX 2 ' ; CATCH .S [rtn] ( 2 -14 ) okIf the extra number added by CATCH is zero, the operation was successful. If the value is not zero, it's an error number. |
Leo: | What did go wrong with this specific example? |
Theo: |
Try the example using EXECUTE instead.
EX 2 ' ; EXECUTE .S [rtn] Compile-only word (message # -14) |
Leo: |
Hm.
(2 minutes of dead silence) The .S following EXECUTE has not executed, but the one after CATCH worked. |
Theo: |
Indeed. EXECUTE cops out.
CATCH catches any errors and restores the stacks, i.e.,
makes sure they have the same depth before and after failed operations.
CATCH also replaces the execution token of the failed word
with the error number.
Of course,
after a successful operation the "allright zero" lands on top of the operation's results. You can verify that with EX 1.
Next one:
EX 3 S" DUP" ' EVALUATE CATCH .S |
Leo: |
This does the same as example 1.
[rtn] ( 3 3 0 ) ok |
Theo: |
Right again. But what about this one:
EX 4 S" fig" ' EVALUATE CATCH .S |
Leo: |
That "fig" can not be evaluated,
so in addition to the 4 there will be some error number on the stack.
[rtn] ( 4 0 0 -61 ) okWhere did those two zeros come from? |
Theo: | You should realize that after an error the stack depth before and after CATCH must be the same. |
Leo: | Oh, of course, the fig-string was on stack. But why has it turned into 0 0 ? |