I have a clojure function which 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 throw it back to the caller -- in this case updateFeedback so that the content in the catch block gets executed. For some reason, clojure logs the exception and the catch block in the caller never executes.
To verify I wrapped the code in the update-document function in a try catch block. There the catch block got executed.
How do I add a throws clause to the function?
Update: I have updated the function. Apologies for the syntax issues. I have updated the code we are using. I am not familiar with clojure. This is a code which we inherited and we are asked to fix a bug. Any pointers would be really helpful.
If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.
If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.
Upon determining that a catch block cannot sufficiently handle an exception, the exception should be rethrown using an empty throw statement.
Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.
If you are trying to catch and then re-throw an 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))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With