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

Handling a file-error.

2 views
Skip to first unread message

Thaddeus L Olczyk

unread,
Feb 28, 2002, 8:59:31 AM2/28/02
to
Can someone suggeswt how to handle a file-error condition?

The best guess I have is:

(handler-bind ((file-error
#'(lambda()

(do-whatever-I-was-going-to-do-if-file-error-generated)))
(basic-processing))

But some of the error handling code I've seen also includes catch
clause, so I'm not quite sure.

Basically what I want to do is process a file. If the processing fails
set some variables to a "default" value. Then continue on and not try
to recover in basic-processing.


Tim Bradshaw

unread,
Feb 28, 2002, 9:18:24 AM2/28/02
to
* Thaddeus L Olczyk wrote:
> Can someone suggeswt how to handle a file-error condition?
> The best guess I have is:

> (handler-bind ((file-error
> #'(lambda()

> (do-whatever-I-was-going-to-do-if-file-error-generated)))
> (basic-processing))

> Basically what I want to do is process a file. If the processing fails


> set some variables to a "default" value. Then continue on and not try
> to recover in basic-processing.

You probably want to use HANDLER-CASE unless you have very specific
reasons to use HANDLER-BIND. HANDLER-CASE is built from HANDLER-BIND,
the basic difference being that the functions that get called by
HANDLER-BIND are called at a point when the condition has been
signaled but not yet handled - the stack has not yet unwound
conceptually. This is useful because it allows these functions to
either decide to handle the condition (which they indicate by doing a
non-local transfer of control, say by throwing or RETURN-FROM or
something like that, or to not handle it. However for most purposes
you want simpler semantics of HANDLER-CASE, whose clauses get called
*after* the stack has unwound. So I think in your case you want
something like this:

(handler-case (process-file)
(file-error (e)
... print information using e perhaps ...
... do any clean up stuff ...
))

--tim



Marco Antoniotti

unread,
Feb 28, 2002, 11:10:24 AM2/28/02
to

Tim Bradshaw <t...@cley.com> writes:

It may be an overkill for C++ and Java trained minds but (just to
prove once again the true power)...


==============================================================================
(defmacro try (form &rest catch-n-finally)
(let ((finally-clause (find "FINALLY" catch-n-finally
:key (lambda (x) (symbol-name (first x)))
:test #'string-equal)))
(if finally-clause
`(unwind-protect
(handler-case ,form
,@(process-catch-forms catch-n-finally))
,@(rest finally-clause))
`(handler-case ,form
,@(process-catch-forms catch-n-finally)))))

(defun process-catch-forms (handler-forms)
;; Each catch form (or finally form) has the following format.
;; (:catch <condition-type> <arglist> <forms>+)
;; (:finally <forms>+)
(loop for (form-key . rest-of-form) in handler-forms
when (string-equal (symbol-name form-key) "CATCH")
collect rest-of-form))
==============================================================================

Therefore.....


(try
(process-file)
(catch file-error (e)
;; ... print information using `e' ...


;; ... do any clean up stuff ...
))

DISCLAIMER: check the code and report errors, thanks.

Cheers

--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488
719 Broadway 12th Floor fax +1 - 212 - 995 4122
New York, NY 10003, USA http://bioinformatics.cat.nyu.edu
"Hello New York! We'll do what we can!"
Bill Murray in `Ghostbusters'.

0 new messages