Dan Liebgold wrote on 02/16/2017 02:33 PM:
How can I rewrite this so that it either eats the exception or is atomic?

I think the difficulty here is that you don't know for certain why a `make-directory` failed, so maybe conditionally re-raising the exception is the way:


(define (make-directory-if-does-not-exist dir)
  (or (directory-exists? dir)
      (with-handlers ((exn:fail:filesystem?
                       (lambda (exn)
                         (if (directory-exists? dir)
;; make-directory exception, but directory exists
                             ;; now, so assume all is OK.
                             (void)
;; make-directory exception, but directory does
                             ;; NOT exist, so re-raise the exception.
                             (raise exn)))))
        (make-directory dir))))


(make-directory-if-does-not-exist "/tmp/my-dir")


BTW, the first `directory-exists?` call is not functionally necessary, but (since the exact low-level behavior is unspecified) a general rule of thumb is that it's gentler to do read first, when that might preclude having to do a write. And you only do the second `directory-exists?` if you already had to do a likely more expensive `make-directory`.

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to