3

This question divides to 2 really :

  • Can i catch an exception with js without using try catch? doesn't have to "catch" it as in to continue executing the code but to be notified that an exception as happend.

Meaning i just want to be notified about exception no matter how thw browser reacts afterwards.

  • Can i wrap code with try/catch during run time?
4

2 回答 2

1

在你的 JS 中的某个地方:

window.someDefinedFunction = function() {
  //Old risky function
}

可以调用该函数的其他任何地方:

window.someDefinedFunctionUnsafe = window.someDefinedFunction;
widnow.someDefinedFunction = function() {
  try {
    window.someDefinedFunctionUnsafe();
  }
  catch {
    //React to error
  }
}

Javascript 全局错误处理是一个蛮力方法的链接。根据您要执行的操作,您可能只想onerror在有风险的通话期间交换唯一的。

于 2012-11-27T13:02:53.757 回答
0

您可以在 javascript 中覆盖 onerror 方法。尝试执行一些代码,这可能是为了通知您已发生错误,其余行将继续执行。

在您的 js 函数中添加以下行以覆盖 onerror 方法。

window.onerror = function() {
alert("你有一个错误");
};

希望能帮助到你。

于 2012-11-27T20:25:12.293 回答