How to catch any Javascript exception in Clojurescript?

前端 未结 3 1319
甜味超标
甜味超标 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:10

    I found another possible answer in David Nolen "Light Table ClojureScript Tutorial"

    ;; Error Handling
    ;; ============================================================================
    
    ;; Error handling in ClojureScript is relatively straightforward and more or
    ;; less similar to what is offered in JavaScript.
    
    ;; You can construct an error like this.
    
    (js/Error. "Oops")
    
    ;; You can throw an error like this.
    
    (throw (js/Error. "Oops"))
    
    ;; You can catch an error like this.
    
    (try
      (throw (js/Error. "Oops"))
      (catch js/Error e
        e))
    
    ;; JavaScript unfortunately allows you to throw anything. You can handle
    ;; this in ClojureScript with the following.
    
    (try
      (throw (js/Error. "Oops"))
      (catch :default e
        e))
    

提交回复
热议问题