CL

Clojure Error Handling

Clojure error handling with ex-info, condition systems, and functional error propagation

Details

Language / Topic
clojureClojure
Category
Error Handling

Rules

balanced
- Use `(ex-info "message" {:type ::my-error :context data})` to create exceptions with structured data instead of bare Java exceptions.
- Use `(try ... (catch ExceptionInfo e (ex-data e)))` to extract the structured map from `ex-info` exceptions and dispatch on `:type`.
- Return `nil` or a sentinel map `{:error/type ::not-found}` from functions that represent expected absent values rather than throwing.
- Use `ex-cause` to access the original exception that caused a wrapped `ex-info` — preserve the original cause when rethrowing at boundary layers.
- Use `(when-let [result (risky-op)] ...)` to handle `nil`-returning functions without explicit nil checks everywhere.
- Use `failjure` or `manifold/deferred` with error channels to propagate errors through pipelines without exception-based control flow.
- Log errors with `clojure.tools.logging` or `timbre` at catch boundaries — include `(ex-data e)` in the log entry for structured context.