2

Is it possible to wrap functions inside a try-catch block? It appears to not work for the first function, but would it work for the prototype function declared that way?

Example:

try {

    function MyFunction1() {

      //function code here

    }

    MyFunction1.prototype.getValue = function() {

      //more code here

   }

} catch (e) {

    //error handling here

}
4

1 回答 1

5

不,不可能以这种方式捕获异常。

函数定义周围的 try/catch 块不会捕获从该函数抛出的异常。

您需要在函数内部或在实际调用该函数的代码周围有一个 try/catch 块。

于 2013-07-09T22:47:13.663 回答