SUMMARY
Error handling in JavaScript is critical for today's complex web applications. Failing to anticipate where errors might occur and how to recover from them can lead to a poor user experience and possibly frustrated users. Most browsers don't report JavaScript errors to users by default, so you need to enable error reporting when developing and debugging. In production, however, no errors should ever be reported this way.
The following methods can be used to prevent the browser from reacting to a JavaScript error:
- The
try-catch
statement can be used where errors may occur, giving you the opportunity to respond to errors in an appropriate way instead of allowing the browser to handle the error. - Another option is to use the
window.onerror
event handler, which receives all errors that are not handled by atry-catch
(Internet Explorer, Firefox, and Chrome only).
Each web application should be inspected to determine where errors might occur and how those errors should be dealt...