Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
脚本块中的javascript错误处理_Javascript_Error Handling_Try Catch - Fatal编程技术网

脚本块中的javascript错误处理

脚本块中的javascript错误处理,javascript,error-handling,try-catch,Javascript,Error Handling,Try Catch,我的页面上有很多脚本: 例如: <script> window.addEvent('Updated', function(data) { /*I'm using a mootools javascript framework and my string below contain an error: Uncaught TypeError: Cannot call method 'empty' of null*/ var container =

我的页面上有很多脚本:

例如:

<script>
  window.addEvent('Updated', function(data) {

    /*I'm using a mootools javascript framework and my string below contain an error: 
      Uncaught TypeError: Cannot call method 'empty' of null*/ 

    var container = document.getElement('cart_contents_data'); 
    container.empty();

  });
</script>

window.addEvent('Updated',函数(数据){
/*我正在使用mootools javascript框架,下面的字符串包含错误:
未捕获的TypeError:无法调用null*/
var container=document.getElement('cart_contents_data');
container.empty();
});
此错误会中断所有其他代码(脚本块)。 我正在寻找隔离脚本块(彼此)的方法

我试过这样的方法:

    <script>
          try {

             window.addEvent('Updated', function(data) {

               /*I'm using a mootools javascript framework and my string below contain an error: 
                 Uncaught TypeError: Cannot call method 'empty' of null*/ 

               var container = document.getElement('cart_contents_data'); 
               container.empty();    
             });

          } catch(err) {console.log(err)}

    </script>

试一试{
window.addEvent('Updated',函数(数据){
/*我正在使用mootools javascript框架,下面的字符串包含错误:
未捕获的TypeError:无法调用null*/
var container=document.getElement('cart_contents_data');
container.empty();
});
}catch(err){console.log(err)}
但是这个解决方案对我没有帮助。 谢谢。

请尝试以下代码:

window.addEvent('Updated', function(data) {
    try {
        var container = document.getElement('cart_contents_data'); 
        container.empty();    
    } catch(err) {
        console.log(err);
    }
});
或者更好的是,为了避免任何例外情况:

window.addEvent('Updated', function(data) {
    var container = document.getElement('cart_contents_data'); 
    if (container != null) {
        container.empty();    
    }
});
问题是code
window.addEvent(..)
仅注册事件处理程序。实际事件发生在后者,可能由javascript框架触发。异常发生在触发事件时,而不是在注册时,这会导致try-catch块根本不工作


在函数中移动
try-catch
块是一种可能的解决方法,但在您的情况下,似乎原因很小,可以通过简单的
if
检查来避免。它提供了更好的性能并提高了代码的可读性。

我知道您的代码很好用。但我需要解决方案来隔离模块代码。可能吗?你说的模块是什么意思。您是否使用requirejs或任何用于Javascript的AMD模块框架?如果是这样,请将此添加到您的问题中(使用“编辑”按钮)不只是结构。@user889349。请参阅我的最新答案。请不要使用单词modules,因为这是不正确的术语。模块不是包含
标记的脚本,它通常称为脚本块。模块是指编程实体,通常在模块框架(GoogleCurl、requirejs或javascript AMD模块)的帮助下构建。在你的情况下,你必须一个接一个地修复你的错误,没有办法对所有的东西进行全局性的尝试,这就是javascript的工作原理。这将允许你在出现错误时调用一些代码。代码完成后,执行仍将停止,脚本的其余部分将无法工作。您必须单独解决每个错误,直到所有错误都得到修复