Wrapping Functions in Try-Catch Block in Javascript

时光毁灭记忆、已成空白 提交于 2019-12-10 23:38:30

问题


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

}

回答1:


No, it's not possible to catch an exception in that way.

A try/catch block around a function definition does not catch exceptions thrown from that function.

You need a try/catch block either inside your function, or around the code that's actually calling the function instead.



来源:https://stackoverflow.com/questions/17559379/wrapping-functions-in-try-catch-block-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!