04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14: Error Handling and Debugging<br />

❑<br />

❑<br />

Chrome — Object name has no method ‘ sort ’ .<br />

Opera — Type mismatch (usually a non - object value used where an object is required).<br />

Although Firefox, Chrome, and Safari at least indicate the part of the code that caused the error, none of<br />

the error messages are particularly clear as to what happened or how it could be fixed. When dealing<br />

with one function, as in the preceding example, debugging is easy enough to handle with these error<br />

messages. However, when you’re working on a complex web application with thousands of lines of<br />

JavaScript code, finding the source of the error becomes much more difficult. This is where a custom<br />

error with appropriate information will significantly contribute to the maintainability of the code.<br />

Consider the following example :<br />

function process(values){<br />

if (!(values instanceof Array)){<br />

throw new Error(“process(): Argument must be an array.”);<br />

}<br />

values.sort();<br />

for (var i=0, len=values.length; i < len; i++){<br />

if (values[i] > 100){<br />

return values[i];<br />

}<br />

}<br />

}<br />

return -1;<br />

In this rewritten version of the function, an error is thrown if the values argument isn ’ t an array. The<br />

error message provides the name of the function as well as a clear description as to why the error<br />

occurred. If this error occurred in a complex web application, you would have a much clearer idea of<br />

where the real problem is.<br />

When you’re developing JavaScript code, take a critical eye towards each function and the circumstances<br />

under which it may fail. A good error handling protocol ensures that the only errors that occur are the<br />

ones that you throw.<br />

Throwing Errors versus try - catch<br />

A common question that arises is when to throw errors versus using try - catch to capture them.<br />

Generally speaking, errors are thrown in the low levels of an application architecture, at a level where<br />

not much is known about the ongoing process and so the error can ’ t really be handled. If you are writing<br />

a JavaScript library that may be used in a number of different applications, or even a utility function that<br />

will be used in a number of difference places in a single application, you should strongly consider<br />

throwing errors with detailed information. It is then up to the application to catch the errors and handle<br />

them appropriately.<br />

The best way to think about the difference between throwing errors and catching errors is this: you<br />

should only catch errors if you know exactly what to do next. The purpose of catching an error is to<br />

prevent the browser from responding in its default manner; the purpose of throwing an error is<br />

to provide information about why an error occurred.<br />

479

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!