How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Clojure programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Clojure programming language

Table of Contents

Problem Statement

Show how to create a user-defined exception   and   show how to catch an exception raised from several nested calls away.

Show/describe what happens when the program is run.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Clojure programming language

Source code in the clojure programming language

(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))


  

You may also check:How to resolve the algorithm Statistics/Basic step by step in the Stata programming language
You may also check:How to resolve the algorithm Stack step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the C++ programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the R programming language
You may also check:How to resolve the algorithm Create a file step by step in the Maple programming language