Exceptions/Catch an exception thrown in a nested call: Difference between revisions

Adds Clojure solution
(Adds Clojure solution)
Line 545:
</pre>
 
 
=={{header|Clojure}}==
<lang clojure>(def U0 (ex-info "U0" {}))
(def U1 (ex-info "U1" {}))
 
(defn baz [x] (if (= x 0) (throw U0) (throw U1)))
(defn bar [x] (baz x))
 
(defn foo []
(dotimes [x 2]
(try
(bar x)
(catch clojure.lang.ExceptionInfo e
(if (= e U0)
(println "foo caught U0")
(throw e))))))
 
(defn -main [& args]
(foo))</lang>
 
{{output}}
<pre>
foo caught U0
 
Exception in thread "main" clojure.lang.ExceptionInfo: U1 {}
at clojure.core$ex_info.invoke(core.clj:4403)
at X.core__init.load(Unknown Source)
...
</pre>
 
The first line of the output is generated from catching the U0 exception in function foo on the first call to bar.
 
On the second call to bar, U1 is caught and re-thrown, which gives a stack trace of the uncaught exception, U1.
 
This example uses clojure.lang.ExceptionInfo, but Java Exceptions can be used instead.
 
=={{header|Common Lisp}}==
Anonymous user