以下代码失败,因为“b”不是有效数字。但是如何抓住它并将日志打印为“导致错误的是b,请修复它”?困难在于如何获得导致失败的当前迭代的值。如何使用try / catch来做到这一点?
=> (map #(Float/parseFloat %) ["1" "b"])
NumberFormatException For input string: "b" sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal.java:2043)
答案 0 :(得分:2)
(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"])