> I am trying to figure out some systematic and clear way how to handle
> exceptions in clojure and their bubbling up through the call chain.
> Let me illustrate it on some code examples (not executable, just to show the
> principle).

One response touched on it briefly, but I'm not sure what problem
you're trying to solve. Usually, in the presence of exceptions, each
layer will be responsible for throwing an exception on error, and you
catch it at the level where it is appropriate. Use of exceptions for
error handling does not imply that you need to sprinkle catches all
over the place.

The annoying exception (no pun intended!) is when you want to convert
exceptions of some type to another at an API boundary, but I don't see
how you will avoid the need to convert error information if that is
what you want to do.

If you mean that your problem is one where the lower-level API uses
return codes because it cannot know whether a particular condition is
a failure condition, I suggest defining an API with an API which *can*
make that assumption, and call that. For example, suppose an HTTP
library provides a convenience function (get ...) which returns an
HTTP response of some kind. Suppose it throws exceptions on I/O errors
and the like. Suppose that in my application I am at a point where I
require an HTTP 200 status response back (any 4xx, 5xx or even 3xx is
an error condition for this particular application). If the HTTP
library does not provide some form of convenience method of expressing
this (such as :expect-status 200), I might define a function similar
to:

  (defn get-expecting [uri status-code]
    (let [response (get uri)]
      (if ....)))

Now I can simply write (get-expecting uri 200) and have it be much
more readable.

In short it allows me to (1) only handle errors where I want to handle
them, rather than sprinkle code anywhere (be it catches or return code
checking) only for the purpose of propagating them up the call chain,
and (2) do so even when the underlying API does not throw exceptions
because it does not make sense for that particular API (such as
read-line).

-- 
/ Peter Schuller

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to