有没有办法在Javascript中的每个函数添加try-catch? [英] Is there a way to add try-catch to every function in Javascript?

查看:238
本文介绍了有没有办法在Javascript中的每个函数添加try-catch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于错误报告,我想在每个函数的代码周围插入一个try-catch包装。



所以基本上我想替换

  function foo(arg){
bar();
}

... with ...

  function foo(arg){
try {
bar()
}
catch(e){
customErrorHandler(e)
}
}

有没有办法将这个通用的try-catch的东西应用到所有的函数,而不需要手动编辑它们呢?例如通过修改Function对象的原型?



编辑



为什么我想尝试捕获我的所有功能:
我正在构建一个我在iOS和Android上发布的HTML5应用程序。我可以从我目前的基本javascript错误报告中得知,即使应用程序在我自己的设备上运行良好,但在其他设备上也会出现错误。



我的目标是双重的:每当某人的设备发生javascript错误,...


  1. 我想通知用户应用程序可能无法正常运行

  2. 我想知道错误发生在哪里,所以我知道在哪里查找问题


解决方案

这不是简单的,因为没有办法找到所有JavaScript定义的函数。例如,任何这样的方法都可能会忽略在运行时定义的回调函数。



您也可能不想包装所有函数,因为这将包括浏览器函数和一个更好的方法可能是定义一个包含另一个函数的函数:

  var tcWrapper = function(f){
return function(){
try {
f.apply ,论据);
} catch(e){
customErrorHandler(e)
}
}
}

现在您可以使用此功能来装饰任何您想要的东西。如果您使用名称空间,包装将变得更加简单:

  var NS = {f:function(...) 。}} 

只需将所有函数包装在一个特殊的命名空间中,然后遍历命名空间: / p>

  $。each(NS,function(i,n){
var p = NS [n];
if(typeof p ==='function'){
NS [n] = tcWrapper(p);
}
});


For error reporting, I would like to insert a try-catch wrapper around the code of every function I have.

So basically I want to replace

function foo(arg){
   bar();
}

...with...

function foo(arg){
    try {
        bar() 
    }
    catch(e){
        customErrorHandler(e)
    }
}

Is there a way to apply this generic try-catch thing to all functions without manually editing all of them? For example by modifying the prototype of the Function object?

EDIT

Why I want to try-catch all my functions: I am building an HTML5 app that I'm publishing on iOS and Android. I can tell from my current rudimentary javascript error reporting that even though the app runs nicely on my own device, errors do occur on some other devices.

My objective is twofold: whenever a javascript error occurs on someone's device...

  1. I want to notify the user that the app may not function perfectly
  2. I want to know roughly where the error occurred, so I know where to look for the problem

解决方案

This isn't simple since there is no way to find all JavaScript function defined everywhere. For example, any such approach would probably miss callback functions which are defined at runtime.

You also probably don't want to wrap all functions because that would include browser functions and functions from JavaScript libraries that you certainly don't want to wrap.

A much better approach is probably to define a function which wraps another function:

var tcWrapper = function(f) {
    return function() {
        try {
            f.apply(this, arguments);
        } catch(e) {
            customErrorHandler(e)
        }
    }
}

Now you can use this function to decorate anything that you want. Wrapping will become more simple if you use name spaces:

var NS = { f: function(...) { ... } }

Just put all functions to wrap in a special namespace and then iterate over the namespace:

$.each( NS, function(i,n) {
    var p = NS[n];
    if( typeof p === 'function' ) {
        NS[n] = tcWrapper(p);
    }
} );

这篇关于有没有办法在Javascript中的每个函数添加try-catch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆