I want my clojure program to have a directory of scripts that it can run - each of these scripts is clojure code that I execute with load-file. This happens within a future so that the script runs in its own thread.
The problem is I never see any error messages from the scripts. If the script fails there's no way to know what went wrong. I assume that's because there is no exception handling within the future's thread. I can put exception handling into the script, like below, and it works:
;; script code
(try
(println (/ 10 0))
(catch Exception e
(println "Exception: " (.getMessage e))))
However, I'd rather put the exception handling a level up from that, around the load-file, so that I don't have to have the exception handling in the script itself:
(defn handleexes [f]
(try
(f)
(catch Exception e
(println "exception: " (.getMessage e)))))
(defn start-script-play [name]
(println "starting script: " name)
(let [f (future (handleexes (load-file (str "./scripts/" name))))]
(swap! scripts (fn [s] (assoc s name f)))))
So there I'm calling the load-file inside handlexes. this doesn't work - mostly. It DOES work when I run a script that contains its own exception handler though, as above! Without the exception handler in the script, nothing. Wierd.
Ok well anyway so my question is what the heck is going on here?
Exception handling is an in-built mechanism in . NET Framework to detect and handle run time errors. Exceptions are defined as anomalies that occur during the execution of a program. The . NET Framework provides a rich set of standard exceptions that are used during exceptions handling.
Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.
The CompletableFuture. join() method is similar to the get method, but it throws an unchecked exception in case the Future does not complete normally.
Why handle exceptions? Exception handling refers to the way that a program handles exceptional circumstances. It is really important that, when an exception occurs, a program does not just 'crash'. This will always be a problem for the end user as it will prevent them from completing the task in hand.
You don't appear to ever be dereferencing (either with deref
or @
) the futures you're producing in your example.
If an exception is thrown within a future, attempting to dereference that future will result in a java.util.concurrent.ExecutionException
being thrown. This exception will wrap whatever was thrown in the future.
(try
(future (/ 10 0))
"done"
(catch Exception e
(str "caught " e)))
;=> "done"
(try
@(future (/ 10 0))
"done"
(catch Exception e
(str "caught " e)))
;=> "caught java.util.concurrent.ExecutionException: java.lang.ArithmeticException: Divide by zero"
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