woodings
woodings

Reputation: 7693

Clojure: How to ignore exceptions that may be thrown from an expression?

I use (try (/ 1 0) (catch Exception e)) but it seems redundant. Is there an easier way to do this? An example where I'm use this is that I do sql/drop-table. It doesn't matter if that call fails because the table doesn't exist.

Upvotes: 3

Views: 1662

Answers (1)

bereal
bereal

Reputation: 34281

How about writing a macro like this:

(defmacro swallow-exceptions [& body]
    `(try ~@body (catch Exception e#)))

(swallow-exceptions (/ 1 0)) ; nil

More sophisticated examples are in this post.

Upvotes: 9

Related Questions