Archive for August, 2007

1063Chapter 39 .Control Structures and (Web hosting plans) Exception Handling break

Friday, August 31st, 2007

1063Chapter 39 .Control Structures and Exception Handling break default : alert( Reload the page and try again. ) } } }

Throwing a Custom Error Object Exception


Enter a number from 1 to 5: Matching Letter is:
If you want to see how the alternative branch of Listing 39-10 looks, copy the list ing file from the CD-ROM to your hard disk and modify the last line of the try block so that one of the letters is dropped from the name of the array: fld.form.output.value = letter[inp] This may simulate the faulty loading of the page. If you enter one of the allowable values, the reload alert appears, rather than the actual message of the error object: letter is undefined. Your users will thank you. All that s left now on this subject are the details on the error object. Error Object Properties Methods Error.prototype errorObject.toString() errorObject.constructor errorObject.description errorObject.filename errorObject.lineNumber errorObject.message errorObject.name errorObject.number errorObject
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

1062 Part IV . JavaScript Core Language Reference (Web site translator)

Friday, August 31st, 2007

1062 Part IV . JavaScript Core Language Reference In Listing 39-10, the getErrorObj() function adds a custom value to the name property of the newly created error object. The name you assign can be any name, but you want to avoid exception names used by JavaScript or the DOM. Even if you don t know what all of those are, you can probably conjure up a suitably unique name for your error. Down in the catch block, a switch construction branches to treat the two classes of errors differently. Notice that because IE5 s error object does not have a name property, the switch expression (e.name) evaluates to undefined, which forces the defaultcase to execute whenever a native exception is thrown (and you have to be careful about which error object properties you use in the defaultcase statements). In this simplified example, about the only possible problem other than the ones being trapped for explicitly in the tryblock would be some corruption to the page during downloading. Therefore, for this example, the branch for all other errors simply asks that the user reload the page and try again. The point is, however, that you can have as many classifications of custom and system errors as you want and handle them in a single catch block accordingly. Listing 39-10: A Custom Object Exception Throwing a Custom Error Object Exception

Throwing an Error Object Exception


Enter a number from 1 to 5: Matching Letter is:
The only difference to the catch block is that it now reads the messageproperty of the incoming error object. This means that if some other exception is thrown inside the try block, the browser-generated message will be displayed in the alert dialog box. In truth, however, the job really isn t complete. In all likelihood, if a browser- generated exception is thrown, the message in the alert dialog box won t mean much to the user. The error message will probably be some kind of syntax or type error the kind of meaningless error message you often get from your favorite operating system. A better design is to branch the catch block so that intentional exceptions thrown by your code are handled through the alert dialog box messages you ve put there, but other types are treated differently. To accomplish this, you can take over one of the other properties of the error object name so that your catch block treats your custom messages separately. throw
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

1060 Part IV . JavaScript Core Language Reference (Free web servers)

Thursday, August 30th, 2007

1060 Part IV . JavaScript Core Language Reference Listing 39-8 (continued) fld.focus() fld.select() } }

Throwing a String Exception


Enter a number from 1 to 5: Matching Letter is:
The flaw with Listing 39-8 is that if some other kind of exception were thrown inside the try block, the value passed to the catchblock would be an error object, not a string. The alert dialog box displayed to the user would be meaningless. Therefore, it is better to be uniform in your throw-catchconstructions and pass an error object. Listing 39-9 is an updated version of Listing 39-8, demonstrating how to create an error object that gets sent to the catchblock via throw statements. The one glitch in generating an error object comes in IE5 and IE5.5. The ECMA-262 standard allows a script statement to set the message property of an error object to directly by passing a string as the parameter to the new Error()constructor. This is how NN6 works. But the error object in IE5 does not have the message property at all, and in IE5.5, the parameter is not assigned to the message property. Therefore, Listing 39-9 contains a separate utility function (getErrorObj()) that fills the gap when an error object does not have the message property to begin with or doesn t have the property set automatically. If a future version of IE adopts the ECMA standard way, then the extra branch is avoided, just as it is for NN6. Listing 39-9: Throwing an Error Object Exception Throwing an Error Object Exception