How to catch any Javascript exception in Clojurescript?

前端 未结 3 1313
甜味超标
甜味超标 2021-02-11 21:56

In my communication layer I have a need to be able to catch ANY javascript exception, log it down and proceed as I normally would do. Current syntax for catching exceptions in C

3条回答
  •  失恋的感觉
    2021-02-11 22:04

    I think I've just found the solution in this link https://groups.google.com/forum/#!topic/clojure/QHaTwjD4zzU

    I copy the contents here: This solution was published by Herwig Hochleitner

    try in clojurescript is actually a macro that uses the builtin try* and adds type dispatch. So to catch everything, just use (try* ... (catch e ...)). This maps directly to javascript's try.

    And here is my implementation working now:

    (defn is-dir? [the_dir]
      (try*
        (if-let [stat (.statSync fs the_dir )]
          (.isDirectory stat)
          false)
        (catch  e 
          (println "catching all exceptions, include js/exeptions")
          false
          )
        )
      )
    

    I hope it helps you
    Juan

提交回复
热议问题