Surprising behavior of Java 8 CompletableFuture exceptionally method

This behavior is specified in the class documentation of CompletionStage (fourth bullet): Method handle additionally allows the stage to compute a replacement result that may enable further processing by other dependent stages. In all other cases, if a stage’s computation terminates abruptly with an (unchecked) exception or error, then all dependent stages requiring its completion … Read more

Why are Exceptions said to be so bad for Input Validation?

Reading these answers, I find it very unhelpful to say, “Exceptions should only be used for exceptional conditions”. This begs the whole question of what is an “exceptional condition”. This is a subjective term, the best definition of which is “any condition that your normal logic flow doesn’t deal with”. In other words, an exceptional … Read more

How can I throw an exception in Clojure?

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 … Read more

Panic recover in Go v.s. try catch in other languages

Panic/Recover are function scoped. It’s like saying that you’re only allowed one try/catch block in each function and the try has to cover the whole function. This makes it really annoying to use Panic/Recover in the same way that java/python/c# etc. use exceptions. This is intentional. This also encourages people to use Panic/Recover in the … Read more

Custom Exceptions in Clojure?

Rather than generating custom classes, there are two much simpler ways to use custom exceptions: Use slingshot – this provides custom throw+ and catch+ macros that let you throw and catch any object, as well as exceptions. In clojure 1.4 and above, you can use clojure.core/ex-info and clojure.core/ex-data to generate and catch a clojure.lang.ExceptionInfo class, … Read more