On Mon, 22 Sep 2003 15:22:25 +0800, jon <[EMAIL PROTECTED]> wrote:

> how can i make my lisp programs exit gracefully? my lisp program
> would hypothetically be a remote number-crunching server where 1
> batch could take days. it would be 'unfortunate' if, say, a user
> keep waiting on the server thinking that it is doing something when
> in fact it has entered the debugger. i've scanned cmu user's manual
> but i can't find nothing like try-catch.

You searched in the wrong place. The CMUCL User's Manual is mainly
about CMUCL's extension to the ANSI Common Lisp language
standard. Common Lisp has perfect error handling capabilities defined
as part of the language. Read the CLHS[1] chapter about "Conditions."
CL also has a THROW/CATCH facility but that's probably not what you
want. You want HANDLER-CASE, or HANDLER-BIND, or maybe you'll want to
have a look at restarts.

A very simple example for "exiting gracefully" would be

  (handler-case
      (my-main-function)
    (error ()
      (ext:quit)))

where MY-MAIN-FUNCTION obviously is your main function. Note that
EXT:QUIT is a CMUCL extension to the standard and you also have
UNIX:UNIX-EXIT which'll allow you to pass a return code to the caller
(which most likely is a shell).

But maybe you don't need any of this. Have you checked the "-batch"
command line option to CMUCL? It will exit Lisp if there's an error
and act like any old Unix program with respect to standard input and
return codes.

  [EMAIL PROTECTED]:/tmp > cat foo.lisp
  (defun foo (x)
    (/ 1 x))

  (print (foo 0))
  [EMAIL PROTECTED]:/tmp > lisp -batch -load foo.lisp
  ; Loading #p"/home/edi/.cmucl-init".
  Error in batch processing:
  Arithmetic error DIVISION-BY-ZERO signalled.
  Operation was KERNEL::DIVISION, operands (1 0).
  [EMAIL PROTECTED]:/tmp > echo $?
  1

Cheers,
Edi.

[1] <http://www.lispworks.com/reference/HyperSpec/Front/index.htm>

Reply via email to