### Macros to deal with exceptions
(silence target expr)(silence substitute target expr)Returns substitute if expr raises an exception that matches
target.
If not provided, substitute is nil.
Target can be:
(silence ArithmeticException (/ 1 0))
=> nil
(silence "Divide by zero" (/ 1 0))
=> nil
(silence [ArithmeticException]
(do (println "watch out !")
(/ 1 0)))
;; watch out !
=> nil
(silence :substitute
(fn [x]
(isa? (class x) ArithmeticException))
(/ 1 0))
=> :substitute
Returns `substitute` if `expr` raises an exception that matches
`target`.
If not provided, `substitute` is `nil`.
Target can be:
- a class
- a sequence of classes
- a predicate
- a string
```clojure
(silence ArithmeticException (/ 1 0))
=> nil
(silence "Divide by zero" (/ 1 0))
=> nil
(silence [ArithmeticException]
(do (println "watch out !")
(/ 1 0)))
;; watch out !
=> nil
(silence :substitute
(fn [x]
(isa? (class x) ArithmeticException))
(/ 1 0))
=> :substitute
```cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |