Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch and rethrow an exception

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.

like image 896
Karthick R Avatar asked Jun 24 '15 11:06

Karthick R


People also ask

How do you Rethrow a caught exception?

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.

Can I Rethrow an exception in Java?

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.

Should you Rethrow an exception?

Upon determining that a catch block cannot sufficiently handle an exception, the exception should be rethrown using an empty throw statement.

How do you catch and throw an exception in Java?

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.


1 Answers

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))))
like image 141
bsvingen Avatar answered Oct 06 '22 00:10

bsvingen