From: Lawrence Troxler
Subject: How do I make errors return nil instead?
Date: 
Message-ID: <616tbb$kca$1@mycroft.westnet.com>
Hi, I hope c.l.l doesn't mind this probably naive question.

Over and over again, I find myself wrapping function calls in constructs
that first check the arguments for validity. Most often, I would be happy
with something that simply returns nil if any of the function args are the
wrong type.

For example, rather than having to write "(if (numerp x) (abs x) nil))",
is there some way in CL of evaluation the form, in this case "(abs x)",
but trapping any errors and returning nil instead of breaking execution?

Isn't there a more general way of doing this? I think I must not be
the only person who is looking for such a feature,
because often in Lisp code, I see functions defined such as
"careful-slot-value", "safe-floor", etc.

Larry


-- 
--  Larry Troxler  --  ··@westnet.com  --  Patterson, NY USA  --
  
From: Barry Margolin
Subject: Re: How do I make errors return nil instead?
Date: 
Message-ID: <617049$6j6@pasilla.bbnplanet.com>
In article <············@mycroft.westnet.com>,
Lawrence Troxler  <··@westnet.com> wrote:
>Over and over again, I find myself wrapping function calls in constructs
>that first check the arguments for validity. Most often, I would be happy
>with something that simply returns nil if any of the function args are the
>wrong type.

(ignore-errors <form>)

will return the value of <form>, or NIL if <form> signals an error.  It
also returns a second value of the error object; this is mostly useful in
cases where NIL is a normal return value of the function and you need to
distinguish the normal NIL (in which case the values will be NIL NIL) from
the error NIL (the values will be NIL non-NIL).

>For example, rather than having to write "(if (numerp x) (abs x) nil))",
>is there some way in CL of evaluation the form, in this case "(abs x)",
>but trapping any errors and returning nil instead of breaking execution?

Of course, you then have to worry about what you're doing with the result
of this form.  Presumably something that was intended to hold an absolute
value is expected to hold a number.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.