JavaScript错误处理的最佳做法是什么? [英] What are the best practices for JavaScript error handling?

查看:98
本文介绍了JavaScript错误处理的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始使我的JavaScript有更多的错误证据,而且我在找到大量有关使用的文档。尝试 catch finally throw ,但是我没有发现大量的建议

I'm looking to start making my JavaScript a bit more error proof, and I'm finding plenty of documentation on using try, catch, finally, and throw, but I'm not finding a ton of advice from experts on when and where to throw errors.

  • Should every piece of code be wrapped in a try/catch?
  • Is there more advice like this on at what point errors ought to be caught?
  • Are there disadvantages to raising errors instead of having code fail silently in production?
  • This has been touched on on SO as far as implementations, but have server-logging JS errors an effective strategy?
  • Anything else I ought to know, regarding trapping errors in my application?

我也完全是为了听到有很好的书的游戏对错误处理进行深入或深入的解释。 Eloquent JavaScript 涉及到这个问题,但对这个问题不是很规范或有意见。

I'm also completely game for hearing of books that have great chapters or in-depth explanations of error-handling. Eloquent JavaScript touches on the matter, but isn't very prescriptive or opinionated about the issue.

感谢您提供的任何建议!

Thanks for any advice you can give!

推荐答案

关于企业JavaScript错误处理的有趣的幻灯片集可以在 http: //www.devhands.com/2008/10/javascript-error-handling-and-general-best-practices/

An immensely interesting set of slides on Enterprise JavaScript Error Handling can be found at http://www.devhands.com/2008/10/javascript-error-handling-and-general-best-practices/

总之,总结:


  1. 假设您的代码将失败

  2. 将错误记录到服务器

  3. 您不是浏览器,而是处理错误

  4. 确定可能发生错误的位置

  5. 抛出您自己的错误

  6. 区分致命与非致命错误

  7. 提供调试模式

  1. Assume your code will fail
  2. Log errors to the server
  3. You, not the browser, handle errors
  4. Identify where errors might occur
  5. Throw your own errors
  6. Distinguish fatal versus non-fatal errors
  7. Provide a debug mode

幻灯片进入更多的细节,最有可能会给你一些方向。

The slides go into much more detail and most probably will give you some direction.

更新

上面提到的演示文稿可以在这里找到:
http://www.slideshare.net/nzakas/enterprise-javascript-error-handling-presentation

The presentation mentioned above can be found here: http://www.slideshare.net/nzakas/enterprise-javascript-error-handling-presentation

这篇关于JavaScript错误处理的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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