From: Thaddeus L Olczyk
Subject: Handling a file-error.
Date: 
Message-ID: <3c8135f6.81152984@nntp.interaccess.com>
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.
 
	

From: Tim Bradshaw
Subject: Re: Handling a file-error.
Date: 
Message-ID: <ey3ofi9k7v3.fsf@cley.com>
* 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
    
 
	
From: Marco Antoniotti
Subject: Re: Handling a file-error.
Date: 
Message-ID: <y6c3czlbn9r.fsf@octagon.mrl.nyu.edu>
Tim Bradshaw <ยทยทยท@cley.com> writes:

> * 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 ...
>         ))

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'.