My question is how can I return a nil from an exception instead of — in the case of using slurp — the file name that would not load and the exception text? Here are the details.
I want the following code to return a nil:
(defn open-csv-file
"Attempts to open a .csv file and complains if the file is not present."
[file-name]
(let [file-data
(try
(slurp file-name)
(catch Exception e (.getMessage e)))]
file-data))
Here is an example of what is being returned now.
bene-cmp.core=> (load-file "src/bene_cmp/core.clj")
#'bene-cmp.core/-main
bene-cmp.core=> (def x (open-csv-file "test_file.csv"))
#'bene-cmp.core/x
bene-cmp.core=> x
"test_file.csv (No such file or directory)"
bene-cmp.core=>
I am trying to avoid modifying this function, so that it throws the exception and then having the caller use a try/catch block.
Thank You.
If I understand your question, you just need to change this:
to this: