且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何跳过或忽略javascript / jquery中的错误?

更新时间:2022-02-13 01:41:31

您可以在代码周围使用try catch块,错误

You can use a try catch block around the code with the error

try{

//code that causes an error

}catch(e){

functionToHandleError(e);
}
//continue from here

但是你应该这样做错误是外部库的已知条件/不可避免的问题,或者仅在错误的用户输入时发生。你不应该用它来让程序不惜一切代价继续运行。

But you should only do that if the error is a known condition/unavoidable problem with an external library or only happens on bad user input. You shouldn't use it to allow the program to keep running at all costs.

避免错误的一些方法


  • 在调用函数之前检查函数是否存在

  • 在引用其属性之前检查对象是否存在

  • 如果用户输入参与,验证输入以检查它是否是预期的类型/范围/等。

  • 如果这是一个加载顺序问题,请考虑重新排序脚本,或者这是一个使用依赖项的更大的应用程序管理系统,如requireJS

  • 如果异步请求后对象未定义,请记住代码不等待异步请求,之后才会定义。处理提供的值的代码应该在回调函数中或使用deffereds / promises来完成。

  • 如果你引用dom元素,考虑从运行时的函数中启动你的JS代码加载DOM,例如jQuery的document.ready或本机window.onload。

  • 如果javascript变量是在没有 var 关键字的情况下声明的或者附加到全局对象,可以在声明之外的地方修改它们。 Javascript也没有块范围。确保您知道变量被修改的所有位置,并尽可能限制变量的范围。特别是避免使用太多的全局变量来保持状态,因为如果没有很多空值/类型检查就很难知道当前状态。

  • check that functions exist before calling them
  • check that an object exists before referencing its properties
  • if user input is involved, validate the input to check that it's the expected type/range/etc
  • if this is a loading order problem consider reordering your scripts, or if this is a larger application using a dependency management system like requireJS
  • if objects are undefined after an asynchronous request, remember that code does not wait on an async request and it will not be defined until afterwards. Code dealing with the provided values should be done in a callback function or using deffereds/promises.
  • if you're referencing dom elements, consider starting your JS code from within a function that runs when the DOM is loaded such as jQuery's document.ready or the native window.onload.
  • if javascript variables are declared without the var keyword, or are attached to a global object, they can be modified in places other than they are declared. Javascript also doesn't have block scope. Make sure you know all the places your variables are being modified, and try to limit the scope of your variables when possible. Especially avoid using too many global variables to hold state, as it can be difficult to know the current state without many null/type checks.

一些处理错误的方法


  • 如果是预期的问题(IE,如果输入可能为空并且需要不同的处理),理想情况下,事先用if语句检查它。如果您不想这样做,可以使用try / catch来转换输入,请求新输入,或者对作为approperiate给出的输入进行操作。

  • If its an expected problem (IE if the input may be empty and requires different handling), ideally check it with an if statement beforehand. If you don't want to do that, you can use try/catch to cast the input, ask for new input, or act on the input given as approperiate.

如果它是一个意外的问题(IE你不知道发生了什么),一个catch块可以是在开发过程中输出诊断信息的好地方。 IE,不要只是抓住它并让它静默失败,输出一条消息让你或其他开发人员知道失败的地方,然后使用你最喜欢的浏览器的web开发工具来检查当时的值,弄清楚发生了什么,并为该案例添加处理以修复它。

if its an unexpected problem (IE you don't know whats going on) a catch block can be a good place to output diagnostic information during development. IE, don't just catch it and let it fail silently, output a message to let you or other developers know where it failed, and then use your favorite browser's web developer tools to inspect the values at the time, figure out what happened, and add handling for that case to fix it.

什么不该做


  • 将try / catch放入,然后让程序继续运行。如果不需要正确运行的代码,请删除它。如果有必要且工作不正常,请修复它。

  • put the try/catch in, and then just let the program keep on running. If the code that isn't running correctly is unnecessary, get rid of it. If it is necessary and isn't working correctly, fix it.

使用try catches作为输入验证(如果函数为null,IE将抛出异常,我会抓住它,然后我知道函数是空值)。尽可能检查是否存在任何预期错误。当行为异常并且您需要关闭替代代码路径或警告用户出现问题时,Try / catches应该处理异常。

use try catches as your input validation (IE if the function is null it will throw an exception and I'll catch it, then I know the function is null). Whenever possible, check for any expected errors. Try/catches should handle "exceptions" when the behavior is unusual and you need to go down an alternative code path or alert the user that something is wrong.