[Newbies] error trap

Ron Teitelbaum Ron at USMedRec.com
Wed Aug 9 16:16:44 UTC 2006


Hi Davide,

I just wrote up something like this for Sedar:
http://lists.squeakfoundation.org/pipermail/beginners/2006-August/000701.htm
l 

To answer your question directly the answer is yes and no.

The easiest way to do what you want is to write your own handler for each
line.  Without doing that you would be counting on an implementation of the
exception.  Not all exceptions are resumable, and it would be difficult to
know what value to resume with that would not cause the error. 

For example:

[10/0.
 -5 raisedTo: 1.5.
 2 raisedToInteger: 1/2.
] on: Exception do: [:ex | Transcript show: ex; cr. ex resume: 1].

Look at the exception ZeroDivide and notice that it is written to be
resumable whereas the others are not.  So resume: 1 works for ZeroDivide and
not for the others.

You could use ifError: on BlockContext for each line to do what you want.

[10/0] ifError: [:ex | Transcript cr; show: ex.].
[ -5 raisedTo: 1.5] ifError: [:ex | Transcript cr; show: ex.]. 
[ 2 raisedToInteger: 1/2] ifError: [:ex | Transcript cr; show: ex.]

If you don't like the amount of extra text on each line you could implement
your behavior on BlockContext (don't tell anyone I said to do this).

BlockContext >> protect
"evaluate the block and print the exception in the transcript if there is an
error"
self ifError: [:ex | Transcript cr; show: ex.]. 

then you could use: 

[10/0] protect.
[ -5 raisedTo: 1.5] protect.
[ 2 raisedToInteger: 1/2] protect.

Notice that this: 
[10/0.
 -5 raisedTo: 1.5.
 2 raisedToInteger: 1/2] protect.

Still doesn't work since the block exits on the first error.

The proper way to do this would be to write your own class to extend
BlockContext instead of changing BlockContext itself.

ProtectBlock class>> evaluate: aBlock
	"evaluate the block and print the exception in the transcript if
there is an error"
	aBlock ifError: [:ex | Transcript cr; show: ex.].

But then you have more writing and clutter, but it is more proper since you
are not cluttering up BlockContext with your transcript print code.

ProtectBlock evaluate: [10/0].
ProtectBlock evaluate: [-5 raisedTo: 1.5].
ProtectBlock evaluate: [ 2 raisedToInteger: 1/2].

Hope that helps some,

Happy coding!

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
Ron at USMedRec.com  


> -----Original Message-----
> From: beginners-bounces at lists.squeakfoundation.org [mailto:beginners-
> bounces at lists.squeakfoundation.org] On Behalf Of Davide Arrigo
> Sent: Wednesday, August 09, 2006 9:54 AM
> To: beginners at lists.squeakfoundation.org
> Subject: [Newbies] error trap
> 
> hi guys, I want to implement an "error trap" in Squeak, something like "on
> error goto:" in other  languages.
> When a method encounter an error during execution of a block of code  (an
> error like: square root or logarithm of a negative numbers, division by
> zero
> and so on..) I want only a message, send to Transcript, without any dialog
> box ask to me if I want to proceed, stop or debug. I wouldn't halt the
> program but always proceed automatically with the next block of code
> evaluation.
> Is it  possible?
> Thanks in advance.
> 
> --
> ---------------
> Davide Arrigo
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners



More information about the Beginners mailing list