208a
<< <> >>

208

The programming language Forth
home

CATCH and THROW I

Leo: Last time we discussed Signed and Unsigned. Today, I'd like you to tell me more about THROW. I know THROW expects a number on the data stack. When this number is zero, nothing happens. Other values cause Forth to abort the program, sometimes after printing "error: message such and such." For example, the result of -14 THROW is
Compile-only word (message # -14)
I'm sure there is more to tell about this mechanism.
Theo: Certainly. But let's take a detour first. You already know about EXECUTE. What do you expect the following does?
17 ' DUP EXECUTE .S
Leo: Easy. ' (Tick) finds DUP, and EXECUTE runs DUP's code. It does the same as a simple DUP.
[rtn] ( 17 17 ) ok
Theo: Indeed. One moment, let me define a word to ... O.K., you can tell me what EX does:
: EX BEGIN DEPTH 0> WHILE DROP REPEAT ;
Leo: In case DEPTH is not zero, numbers are dropped off the stack. Thus EX cleans up the stack.
Theo: O.K. Here's Puzzle # 1. What does this do:
EX 1 ' DUP CATCH .S
Let me add that in this case CATCH has almost the same effect as EXECUTE. The difference is that CATCH additionally places a zero on the stack. Deduce the answer before pressing [rtn].
>>