如何在Clojurescript中捕获任何Javascript异

如何在Clojurescript中捕获任何Javascript异

本文介绍了如何在Clojurescript中捕获任何Javascript异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的通信层中,我需要能够捕获任何JavaScript异常,将其记录下来,并按照通常的方式进行操作。
Clojurescript中捕获异常的当前语法规定我需要指定被捕获的异常的类型。

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 Clojurescript dictates that I need to specify the type of the exception being caught.

我试图使用nil,js / Error, js / object在catch窗体中,它没有捕获任何JavaScript异常(可以将字符串作为对象的类型)。

I tried to use nil, js/Error, js/object in the catch form and it doesn't catch ANY javascript exception (which can have string as the type of the object).

我会欣赏任何提示如何这可以在Clojurescript中本地完成。

I would appreciate any hints how this can be done natively in Clojurescript.

推荐答案

我在David Nolen发现了另一个可能的答案

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))

这篇关于如何在Clojurescript中捕获任何Javascript异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:54