I wish to throw an exception and have the following:
(throw "Some text")
However it seems to be ignored.
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.
We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.
The throws keyword can be useful for propagating exceptions in the call stack and allows exceptions to not necessarily be handled within the method that declares these exceptions. On the other hand, the throw keyword is used within a method body, or any block of code, and is used to explicitly throw a single exception.
Controller Based Exception HandlingYou can add extra ( @ExceptionHandler ) methods to any controller to specifically handle exceptions thrown by request handling ( @RequestMapping ) methods in the same controller.
You need to wrap your string in a Throwable:
(throw (Throwable. "Some text"))
or
(throw (Exception. "Some text"))
You can set up a try/catch/finally block as well:
(defn myDivision [x y] (try (/ x y) (catch ArithmeticException e (println "Exception message: " (.getMessage e))) (finally (println "Done."))))
REPL session:
user=> (myDivision 4 2) Done. 2 user=> (myDivision 4 0) Exception message: Divide by zero Done. nil
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