JavaScript错误处理的最佳做法是什么? [英] What are the best practices for JavaScript error handling?
问题描述
我正在开始使我的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.
- 每个代码都应该包含在try / catch中吗?
- 有更多建议,如
- 生产中出现错误而不是代码失败,是否有缺点?
- 对于实现而言,这已经在SO上被触及,但服务器日志记录JS错误是一个有效的策略?
- 我应该知道的其他任何事情,关于在我的应用程序中捕获错误?
- 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/
总之,总结:
- 假设您的代码将失败
- 将错误记录到服务器
- 您不是浏览器,而是处理错误
- 确定可能发生错误的位置
- 抛出您自己的错误
- 区分致命与非致命错误
- 提供调试模式
- Assume your code will fail
- Log errors to the server
- You, not the browser, handle errors
- Identify where errors might occur
- Throw your own errors
- Distinguish fatal versus non-fatal errors
- 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屋!