catch & throw (ATTN: Dave Ungar & co.)

burdick at mentor.cc.purdue.edu burdick at mentor.cc.purdue.edu
Wed Dec 12 08:20:26 UTC 1990


Dave & co.: I didn't see catch and throw in the docs, so if you guys
haven't put catch & throw in the standard world, you might want to
just put this code in there (at least I don't know of any rules of
good programming it violates or anything)...

Maybe this has been discussed in this group already, but just in case,
here's a useful addition to the startup.self...

------------

traits block _AddSlots: (|
	^ catch = (value: throwBlock with: [|:throwValue| ^throwValue]).
	_ throwBlock = (|
		^ with: aBlock = (_Clone block: aBlock).
		^ throw: aValue = (block value: aValue).
		^ throw = (throw: nil).
		_ block.
	|).
|).

------------

You can use it like this:

[|:errorTag|
 message: errorTag
] catch.

message: errorTag = (
	errorTag throw: 'oops!'.
	'this code never happens!' print.
).


The reason I defined throwBlock instead of just putting throw in block
along with catch is to help with errors; to make sure that only
throwBlocks receive the throw[:] message and not just any old block.

If this doesn't make sense, any CommonLISP book should have a good
explanation. I could never figure out why ParcPlace never included
catch and throw in their release; it's easy to add and it's great for
handling errors.

	-- Bill
	burdick at mentor.cc.purdue.edu

P.S.  There may be a syntax error or two in this code -- I don't have
self (I guess I'm trying to find my self :) ), yet, so I haven't been
able to test this.



More information about the Self-interest mailing list