ClojureDocs

Nav

Namespaces

The exprs are evaluated and, if no exceptions occur, the value of the last
is returned. If an exception occurs and catch clauses are provided, each is
examined in turn and the first for which the thrown exception is an instance
of the named class is considered a matching catch clause. If there is a
matching catch clause, its exprs are evaluated in a context in which name is
bound to the thrown exception, and the value of the last is the return value
of the function. If there is no matching catch clause, the exception
propagates out of the function. Before returning, normally or abnormally,
any finally exprs will be evaluated for their side effects. See
http://clojure.org/special_forms for more information.
3 Examples
=> (try
     (/ 1 0)
     (catch Exception e (str "caught exception: " (.getMessage e))))

"caught exception: Divide by zero"
;; multiple catch clauses which handle different exceptions

=>  (let [divisor [2 0 "clojure"]]
      (try
        (/ 4 (rand-nth divisor))
        (catch ArithmeticException e
          (println "Probably trying to divide by zero...")
          111)
        (catch ClassCastException e
          (println "Did you try to do math with a string?")
          222)
        (catch AssertionError e
          (println "Some conditions aren't compatible with the requirements of a function")
          333)
        (catch Exception e
          (println "Some other exception, won't be caught in this case...")
          444)
        (finally
          (println "Always executed but won't return a value!"))))

;; first case
Always executed but won't return a value!
=> 2

;; second case
Probably trying to divide by zero...
Always executed but won't return a value!
=> 111

;; third case
Did you try to do math with a string?
Always executed but won't return a value!
=> 222

;; forth case
Some conditions aren't compatible with the requirements of a function
Always executed but won't return a value!
=> 333

;; fifth case
Some other exception, won't be caught in this case...
Always executed but won't return a value!
=> 444
;; Note that in ClojureScript, one can use (catch :default e ...) to catch any 
;; type of value, roughly equivalent to (catch Exception e ...) in JVM Clojure.

(try
  (/ 2 (rand-nth [0 "oops"]))
  (catch :default e             ; Note: ClojureScript only!
    (println "Error!" e)))

;; This is not currently possible in JVM Clojure, though the ticket CLJ-1293 
;; proposes adding the same behavior there and has some background info.
See Also

The exprs are evaluated and, if no exceptions occur, the value of the last is returned. If an except...

Added by zk

The exprs are evaluated and, if no exceptions occur, the value of the last is returned. If an except...

Added by zk

The expr is evaluated and thrown, therefore it should yield an instance of some derivee of Throwable...

Added by MicahElliott

Returns exception data (a map) if ex is an IExceptionInfo. Otherwise returns nil.

Added by MicahElliott

Create an instance of ExceptionInfo, a RuntimeException subclass that carries a map of additional...

Added by MicahElliott

Prints a Clojure-oriented stack trace of tr, a Throwable. Prints a maximum of n stack frames (defa...

Added by MicahElliott
0 Notes
No notes for catch