Build Status Dependency Status

slingshot

Enhanced throw and catch for Clojure

Provides try+ and throw+. Each is 100% compatible with Clojure and Java's native try and throw both in source code and at runtime. Each also provides additional capabilities intended to improve ease of use by leveraging Clojure's features like maps, records, and destructuring.

Clojure's native try and throw behave much like those of Java: throw can accept objects derived from java.lang.Throwable and try selects from among catch clauses based on the class of the thrown object.

In addition to fully supporting those uses (whether they originate from Clojure code or from Java code via interop), try+ and throw+ provide these enhanced capabilities:

Usage

project.clj

Clojars Project

tensor/parse.clj

```clojure (ns tensor.parse (:use [slingshot.slingshot :only [throw+]]))

(defn parse-tree [tree hint] (if (bad-tree? tree) (throw+ {:type ::bad-tree :tree tree :hint hint}) (parse-good-tree tree hint))) ```

math/expression.clj

```clojure (ns math.expression (:require [tensor.parse] [clojure.tools.logging :as log]) (:use [slingshot.slingshot :only [throw+ try+]]))

(defn read-file [file] (try+ [...] (tensor.parse/parse-tree tree) [...] (catch [:type :tensor.parse/bad-tree] {:keys [tree hint]} (log/error "failed to parse tensor" tree "with hint" hint) (throw+)) (catch Object _ (log/error (:throwable &throw-context) "unexpected error") (throw+)))) ```

Credits

Based on clojure.contrib.condition, data-conveying-exception, discussions on the clojure mailing list and wiki and discussions and implementations by Steve Gilardi, Phil Hagelberg, and Kevin Downey.

License

Copyright © 2011-2014 Stephen C. Gilardi, Kevin Downey, and Phil Hagelberg

Distributed under the Eclipse Public License, the same as Clojure.