Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

catching floating point exceptions in lisp

36 views
Skip to first unread message

Barry Margolin

unread,
Oct 9, 1997, 3:00:00 AM10/9/97
to

In article <343D4E...@ics.uci.edu>,
Stephen Dongjun Bay <sb...@ics.uci.edu> wrote:
>Does anybody know how to catch floating point exceptions in Lisp?

See the chapter of CLtL2 on the Condition System. You can establish a
handler for ARITHMETIC-ERROR, DIVIDE-BY-ZERO, FLOATING-POINT-OVERFLOW,
and/or FLOATING-POINT-UNDERFLOW.

Or if you just want the expression to return NIL if it gets an error, use
the IGNORE-ERRORS macro.

--
Barry Margolin, bar...@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.

Erik Naggum

unread,
Oct 10, 1997, 3:00:00 AM10/10/97
to

* Stephen Dongjun Bay
| Occaisonally divide by zero errors occur and stops program execution.
| Instead of stoping my lisp program I would like to handle the divide by
| zero errors automatically (i.e. I can throw away the expressions because
| they are invalid) and continue.

if the expression is truly uninteresting when it contains such an error,
you can wrap the call to `eval' in an `ignore-errors' form. the primary
value is then nil if an error occurred. you may check the secondary value
for any condition that might have occurred. otherwise, you might want to
consider setting up a handler for the condition `arithmetic-error'. you
also gain access to the operands and the operation that caused this
condition.

(handler-case (+ (/ 3 4) (/ 1 0))
(arithmetic-error (x)
(format t "~S bit the proverbial bullet"
(cons (arithmetic-error-operation x)
(arithmetic-error-operands x)))))
-| (/ 1 0) bit the proverbial bullet
=> nil

#\Erik
--
if you think this year is "97", _you_ are not "year 2000 compliant".

see http://www.naggum.no/emacs/ for Emacs-20-related material.

0 new messages