Books
in black and white
Main menu
Home About us Share a book
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 79 80 81 82 83 84 < 85 > 86 87 88 89 90 91 .. 118 >> Next

A problem with a user’s browser, operating system, or hardware configuration
Trying to make your code access objects (such as array elements, properties, files, and so on) that don’t exist is a common source of exceptions that might occur while your JavaScript code is executing in someone’s browser.
If you’re creating a commercial JavaScript application, you want to make liberal use of JavaScript’s exception-handling abilities. Allowing your users to view cryptic, system-generated errors such as File Not Found or No Such Object is unacceptable in a commercial environment. Although anticipating
250 Part IV: Interacting with Users
and handling those errors by using try and catch blocks might not prevent the errors from occurring, it does give you the opportunity to
^ Reassure users. You can use JavaScript’s exception-handling functions to display a message telling users that an error has occurred but is being handled appropriately. (This approach is much better than allowing a cryptic system message or blank screen to confuse and alarm users.)
^ Provide users with helpful, appropriate suggestions. You can explain the cause of the error and provide users with tips for avoiding that error in the future.
Handling Exceptions
^tco
You handle exceptions by creating two special JavaScript functions, or blocks: a try block and a catch block. Then, in any statement that might generate an error, you use the keyword throw to throw an error to the catch block. The code in Listing 14-1 shows you how.
Look for the code in Listing 14-1 in the file list1401.htm on the companion CD.
Listing 14-1: Handling Exceptions with try-catch and throw
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
function getMonthName (monthNumber) {
// JavaScript arrays begin with 0, not 1, so // subtract 1.
monthNumber = monthNumber - 1
// Create an array and fill it with 12 values var months = new Array(”Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”, "AugV'SepV'Oct'V'NovV'Dec'')
// If the monthNumber passed in is somewhere // between 0 and 11, fine; return the corresponding // month name.
if (months[monthNumber] != null) { return months[monthNumber]
}
// Otherwise, an exception occurred, so throw // an exception.
Chapter 14: Handling Runtime Errors 251
Figure 14-1:
Houston, we have an error.
else {
// This statement throws an error // directly to the catch block. throw "InvalidMonthNumber”
}
}
//////////////////////////////////////////////////////
// The try block wraps around the main JavaScript // processing code. Any JavaScript statement inside // the try block that generates an exception will // automatically throw that exception to the // exception handling code in the catch block. //////////////////////////////////////////////////////
// The try block try {
// Call the getMonthName() function with an // invalid month # (there is no 13th month!)
// and see what happens.
alert(getMonthName(13))
alert("We never get here if an exception is thrown.”)
}
// The catch block catch (error) {
alert("An " + error + " exception was encountered. Please contact the program vendor.”)
// In a real-life situation, you might want // to include error-handling code here that // examines the exception and gives users specific // information (or even tries to fix the problem,
// if possible.)
}
Take a look at Figure 14-1 to see the error that running the code in Listing 14-1 generates in Internet Explorer.
Щ
•\ An InvalidMonthNumber exception was encountered. Please contact the
• \ program vendor.
1 OK 1
252 Part IV: Interacting with Users
The first code executed in Listing 14-1 is the code that you see defined in the try block:
alert(getMonthName(13))
Because only 12 months are defined in the months array, passing a value
of 13 to getMonthName() causes an exception ("InvalidMonthNumber") to be thrown, as shown here:
function getMonthName(monthNumber) { throw "InvalidMonthNumber”
All thrown exceptions are processed automatically by whatever code exists in the catch block, so the message that you see in Figure 14-1 (and defined in the catch block code shown in Listing 14-1) appears automatically when the exception is thrown.
If you want to write truly airtight JavaScript code, you need to identify all the events that could possibly cause an exception in your particular script (such as actions the user could take, error conditions the operating system could generate, and so on), and implement a try-catch block for each.
Depending on your application, you might want to include more processing code in the catch block than the simple pop-up message shown in Figure 14-1. For example, you might want to include JavaScript statements that examine the caught exception, determine what kind of exception it is, and process it appropriately.
You aren’t limited to a string literal when it comes to identifying a thrown exception. Instead of InvalidMonthNumber, you can create and throw an elaborate custom exception object (by using the function and new operators that I describe in Chapter 3).
Previous << 1 .. 79 80 81 82 83 84 < 85 > 86 87 88 89 90 91 .. 118 >> Next