How to catch and reverse engineer an exception

I have a clojure function that calls another function to update the database.

(^{PUT true
       Path "/{id}"
       Produces ["application/json"]
       Consumes ["application/json"]
       ApiOperation {:value "Update" :notes ""}}
       updateFeedback [this
                       ^{PathParam "id"} id
                       body]
       (require 'com.xx.x.xx.xx.xx-response)
       (let [doc (json/read-json body)]
         (if-let [valid-doc (validate doc)]
                 (try+
                  (->>
                   (assoc valid-doc :modificationDate (Utilities/getCurrentDate))
                   (couch/update-document dbs/xx-db)
                   (core/ok-response))
                  (catch java.io.IOException ex
                         (log/error "line num 197"))
                  (catch java.lang.Exception ex
                         (log/error "line num 200"))))))

      

The update document function throws an exception. I would like to return it back to the caller - in this case updateFeedback so that the content in the catch block is executed. For some reason, clojure logs an exception and the catch block in the caller is never executed.

To test, I wrapped the code in update-document functions in a try catch block. The catch block was captured there.

How do I add a throw clause to a function?

Update: I've updated the function. Apologies for syntax problems. I have updated the code we are using. I am not familiar with clojure. This is the code we inherited and we will be asked to fix the error. Any pointers would be really helpful.

+3


source to share


1 answer


If you are trying to catch and then re-throw the exception, you can do something like this:



(defn throwing-function
  []
  (/ 7 0))

(defn catching-function
  []
  (try
    (throwing-function)
    (catch Exception e
      (println "Caught exception:" e)
      (println "Re-throwing ...")
      (throw e))))

      

+7


source







All Articles