使用try catch for map时如何获取当前迭代的值
how to get the value of the current iteration when using try catch for map
以下代码失败,因为 "b" 不是有效数字。但是如何捕获它并将日志打印为 "It is b which cause the error, please fix it"?困难在于如何获取导致失败的当前迭代的值。如何使用 try/catch 来做到这一点?
=> (map #(Float/parseFloat %) ["1" "b"])
NumberFormatException For input string: "b" sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal.java:2043)
(defn parse-incredulously [x]
(try (Float/parseFloat x)
(catch Exception e
(println (format "It is %s which cause the error, please fix it" x)))))
(map parse-incredulously ["1" "b"])
以下代码失败,因为 "b" 不是有效数字。但是如何捕获它并将日志打印为 "It is b which cause the error, please fix it"?困难在于如何获取导致失败的当前迭代的值。如何使用 try/catch 来做到这一点?
=> (map #(Float/parseFloat %) ["1" "b"])
NumberFormatException For input string: "b" sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal.java:2043)
(defn parse-incredulously [x]
(try (Float/parseFloat x)
(catch Exception e
(println (format "It is %s which cause the error, please fix it" x)))))
(map parse-incredulously ["1" "b"])