WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[next]

Debugging JavaScript: Handling Runtime Exceptions

By

Social Bookmark

JAVA Developer – Trading Industry (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume


It's surprising how many scripts don't incorporate proper exception handling. You can be as careful in your coding as you want, but sooner or later, something unexpected will come up and cause an exception to rear its ugly head! The Error object, available since JavaScript 1.5, is the solution. In Internet Explorer 5+ and Mozilla 1.0+, it provides a number of properties to help you display more meaningful error messages to the client and debugging information that is more useful to you, the developer. In the Debugging JavaScript: Understanding JavaScript Error Messages tutorial, we looked at the nature of exceptions in applications and discussed some of the more commonly encountered JavaScript errors. Today, we'll go over how to use the Error object to handle runtime exceptions.

The Try-Catch Block

The syntax of the try-catch block is ported over from the Java language. Any errors generated by code contained within the try's braces will execute the catch block, allowing for an alternate action or a graceful failure to occur. At the very least, a user-friendly message can be constructed and displayed to the client. Here is the basic syntax:

In the following example, the eval() function fails because the string won't evaluate to proper JavaScript code. Instead of allowing the browser to display some cryptic message or do nothing at all, we'll use an alert() to display a customized message:

And Finally...

The finally clause can be used to perform tasks, regardless of whether or not there was an exception. It's a good place to perform clean up or continue with procedures that don't depend on the contents of the try-catch block:

Be aware that there is nothing that can be done in the try-catch block to stop the finally from executing, including return statements. Therefore, the following should be avoided:

See the "Throwing Your Own Errors" section for a good example of the finally statement.

Exception Handling Anti-Patterns

An anti-pattern is a practice of coding something that appears obvious but is in fact ineffective or far from optimal in practice. Hence, anti-patterns ultimately produce more nasty side effects than benefits. With respect to exception handling, anti-patterns can emerge from consistent treating of errors as an afterthought to getting the script up and running smoothly.

Error Swallowing

Error Swallowing, or Error Hiding, results from the noble desire to avoid an application meltdown by dealing with it in the main function block or from passing partial information to an error handler sub routine. For instance, in the following code, the original error is lost because only the message is passed up the calling chain:

Another questionable use of the try-catch block is called the "do nothing" approach. Its defining feature is an empty catch block or one that contains a comment along the lines of "do nothing" or "ignore errors". In the following example, the coder has determined that the absence of the result string does not qualify as an error. To avoid an error condition, the individual checks for the result in the main code block and exits the function prematurely:

Although the developer has shown initiative by proactively handling a potential error, it will be difficult to debug the code in the event of abnormal behavior, as no error information is conveyed. Other problems with the above code are that:


[next]

Recent Articles

WebReference.com site name
Use Web Caching to Make Your Web Site Faster
Creating an Online Shopping Cart Mechanism in PHP
Log JavaScript Errors Using an AJAX-driven Web Service
internet.com site name
Configuring Granular Settings for a Database Level Audit
The Perils of a Web 2.0 Transition on Your Business Processes
Facebook Redesigns Site —Again — Nears 400M Mark



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers