[racket] Handling errors from in-directory - and handling errors more generally

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Oct 14 08:42:32 EDT 2010

At Thu, 14 Oct 2010 11:15:24 +0100, Peter Kiggins wrote:
> On 12 October 2010 16:50, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> > At Tue, 12 Oct 2010 14:48:49 +0100, Peter Kiggins wrote:
> >> [I am completely new to both Scheme and racket, so if my questions are
> >> Looking at traversal of a file tree led me to in-directory.
> >>
> >> [ ... ]
> >>
> > No particular restart system built into the exception system. Code that
> > raises an exception can include a continuation in the exception to
> > allow a restart, and the exception raised for an asynchronous break
> > (which is usually triggered when you hit Ctl-C) does that. But
> 
> I'm not doing well understanding the Racket exceptions/continuations
> model!  I'll go read some more...
> 
> ...some very elementary examples of using continuations (and
> exceptions) would be very helpful...?

Here's one:

 http://groups.google.com/group/plt-scheme/browse_thread/thread/605fb9bf182e8e7f

> > `directory-list' as used by `in-directory' doesn't include a restart
> > continuation in the exception record that it creates, and
> > `in-directory' doesn't particularly catch exceptions or introduce any
> > restart continuations.
> 
> I think 'in-directory' doesn't do what I want. I'm struggling to see
> how it is useful in practice if it barfs on errors and can't be
> re-started or told how to handle errors?

It's useful only in the simple case where there aren't errors. (My own
uses have been in little scripts on my own filesystem, where I knew
that the directories were readable and weren't going to change as the
script runs.)

For uses that are more complicated, my instinct is much like
Matthias's: `in-directory' is just a convenience, so when it fails to
be convenient, I write the extra few lines of code to create my own
loop where I have control over the details.

> > Among Racket procedures that support specific failure handling, most do
> > so through an optional failure-thunk argument. For example, `hash-ref'
> > and `file-or-directory-modify-seconds' accept failure thunks. The
> > handlers in those cases just produce an alternate result --- the same
> > thing could be accomplished by catching an exception --- as opposed to
> > continuing internally. So, I think there's not really much precedent
> > for this in the Racket API.
> 
> So standard idiom is optional arg for failure-thunk? And a re-start would
> require that the procedure had provided one or more continuations to
> explicitly handle re-starts?

No, I think that's mixing up different idioms.

If `in-directory' took some sort of procedure to deal with failures,
probably the protocol for that procedure would not explicitly involve
continuations. More likely, the procedure would accept an exception
record for a directory-read failure, and the procedure would either
re-raise the exception or return an alternate list of paths for the
directory's content.



Posted on the users mailing list.