Way of indicating an error

Ramon Leon ramon.leon at allresnet.com
Mon Feb 26 16:09:58 UTC 2007


> Hi,
> 
> if a method should compute and return a value. If this method 
> detects an error in its process what is the best way to indicate it:
> 
> - returning nil instead of the computed value
> - raise a specific error
> 
> 
> Bye
> 
> --
> Damien Cassou

I like how detect works.  If you don't pass a handler, raise an error

^#(John Doe) detect: [:each | each = 'Ramon'] => Error: Object is not in the
collection

If you want to handle the error, don't catch it, make another method that
takes the handler as an arg so the api is cleaner, so the client doesn't
have to catch it.

^#(John Doe) detect: [:each | each = 'Ramon'] ifNone:['']

Then you can return nil via the handler if you like, or just handle the
error some other way like returning an empty string, or don't pass a handler
and let the exception propagate.

Ramon Leon
http://onsmalltalk.com




More information about the Squeak-dev mailing list