【问题标题】:Hide JS error without custom error handler在没有自定义错误处理程序的情况下隐藏 JS 错误
【发布时间】:2012-03-13 14:15:52
【问题描述】:

我有一行代码抛出了一个我不关心的错误。我需要从控制台隐藏它。我不想使用自定义错误处理程序并返回 false,因为我想查看所有其他错误。

我尝试了 try/catch,但错误仍然显示在控制台中:

try{
    //the erroneous line of code
}catch(err){ }

我也尝试过在错误行之前摆弄 window.onerror,并在之后立即摆弄它,但在这种情况下仍然会出现错误:

window.onerror = function(){ return false; }
//erroneous line of code
window.onerror = function(){ return true; }

任何帮助将不胜感激。

【问题讨论】:

  • 错误是什么?代码是什么样的?你尝试过什么(证明)?
  • 你能发布你的代码吗?通常尝试/赶上工作。

标签: javascript error-handling


【解决方案1】:

try catch 是一种使用方法

b=a; // gives an error in the console
try {
  a = x; // does NOT give an error in the console
}
catch(e) {
  document.getElementById('error').innerHTML='ERROR: '+e.message;
}  

我只会得到

a 没有定义

在我的fiddle控制台中

我没有在控制台中收到 SECOND 错误。甚至在注释掉第一个错误以防它可能阻止进一步处理之后也不行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 2018-04-29
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2011-04-21
    相关资源
    最近更新 更多