### Enhanced Exception Handling with Clojure's slingshot try+ and throw+ Source: https://github.com/scgilardi/slingshot/blob/release/README.md This documentation outlines the core enhancements provided by `slingshot`'s `throw+` and `try+` over standard Clojure/Java exception mechanisms. `throw+` allows throwing any Java object, including Clojure maps or records, simplifying custom exception creation. `try+` can catch a wider range of thrown objects, including non-Throwables and maps passed to `ex-info`, and offers flexible selector mechanisms for catch clauses. ```APIDOC throw+: - Can throw any Java object (not just Throwable). - Clojure maps or records become an easy way to represent custom exceptions without requiring gen-class. try+: - Catch any Java object thrown by throw+. - Catch any map passed to ex-info and thrown by throw or throw+. - Catch any Throwable thrown by Clojure's throw or Java's throw. - The first catch clause whose selector matches the thrown object will execute. ``` -------------------------------- ### Accessing Throw Context with slingshot &throw-context Source: https://github.com/scgilardi/slingshot/blob/release/README.md The `&throw-context` map provides detailed information about the context at the throw site within a `catch` clause. This documentation outlines the structure of `&throw-context` for both `Throwable` and non-`Throwable` caught objects, including keys for the object itself, message, cause, stack trace, and wrapper information. ```APIDOC for Throwable caught objects: :object the caught object; :message the message, from .getMessage; :cause the cause, from .getCause; :stack-trace the stack trace, from .getStackTrace; :throwable the caught object; for non-Throwable caught objects (including maps passed to ex-info): :object the caught object; :message the message, from throw+ or ex-info; :cause the cause, from throw+ or ex-info, see below; :stack-trace the stack trace, captured by throw+ or ex-info; :wrapper the Throwable wrapper that carried the object; :throwable the outermost Throwable whose cause chain contains the wrapper, see below. ``` -------------------------------- ### Catching Custom Exceptions and Logging in Clojure Source: https://github.com/scgilardi/slingshot/blob/release/README.md Illustrates how to use `slingshot.slingshot/try+` to catch specific custom exceptions, such as `:tensor.parse/bad-tree`, and log error details using `clojure.tools.logging`. It also includes a generic catch-all for unexpected errors. ```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+)))) ``` -------------------------------- ### Understanding Catch Clause Selectors in slingshot try+ Source: https://github.com/scgilardi/slingshot/blob/release/README.md Within a `try+` block, `catch` clauses use selectors to determine which thrown objects they handle. This documentation details the various types of selectors available: class names, key-values vectors, predicates, and general selector forms, explaining how each matches a thrown object. ```APIDOC Catch Clause Selectors: - Class Name: (e.g., RuntimeException, my.clojure.record) Matches any instance of that class. - Key-Values Vector: (e.g., [key val & kvs]) Matches objects where (and (= (get object key) val) ...). - Predicate: (function of one argument like map?, set?) Matches any Object for which the predicate returns a truthy value. - Selector Form: A form containing one or more instances of `%` to be replaced by the thrown object. Matches any object for which the form evaluates to truthy. ``` -------------------------------- ### slingshot try+ Catch Selector Form Equivalents Source: https://github.com/scgilardi/slingshot/blob/release/README.md This snippet illustrates how shorthand selectors used in `try+`'s `catch` clauses are internally transformed into their equivalent, more explicit selector forms. The `%` symbol represents the thrown object in these transformations. ```Clojure => (instance? %) [ & ] => (and (= (get % ) ) ...) => ( %) ``` -------------------------------- ### Using the Optional else Clause in slingshot try+ Source: https://github.com/scgilardi/slingshot/blob/release/README.md The `try+` construct in slingshot supports an optional `else` clause. This documentation describes its placement and behavior: it executes only if no exception is thrown from the `try+` body, providing a mechanism for side effects when the primary code path completes successfully. ```APIDOC try+ Optional else Clause: - Placement: After all catch clauses and before any finally clause. - Execution: Contents executed only if nothing was thrown from the try+ body. - Purpose: For side effects when no exception occurs. ``` -------------------------------- ### Throwing Custom Exceptions in Clojure with Slingshot Source: https://github.com/scgilardi/slingshot/blob/release/README.md Defines a `parse-tree` function that throws a custom `::bad-tree` exception using `slingshot.slingshot/throw+` when an invalid tree structure is encountered. The exception includes the tree and a hint for debugging. ```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))) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.