Appendix C
JavaScript™ Code for Error Handling
Read the directions carefully and write JavaScript code that addresses the requirements. Copy your code directly into the Appendix document and then post to your Individual forum.
1. Write JavaScript code that anticipates and handles an error for an expected numeric field. This code is executed on keypress and the entered value is saved for you in a variable called enteredChar. Include the try block of JavaScript statements needed to check if the character is not a number or a non-alphanumeric character or if you throw an error message. [pic]
Error Handling
function handleError(keyPressEvent) { var enteredKey; if (navigator.appName == "Microsoft Internet Explorer") enteredKey = keyPressEvent.keyCode; else if (navigator.appName == "Netscape") enteredKey = keyPressEvent.charCode; var enteredChar = String.fromCharCode(enteredKey); var allowChar = true; try { if ( isNaN(enteredChar)) { throw "You did not enter a numeric value."; allowChar = false; } } catch( error) { window.alert(error); return allowChar; } finally { return allowChar; } }
Error Handling in Java Script
[pic]
2. Write a custom error handling JavaScript function called processErrors that handles a custom error by assigning it to the onerror event handler. Include the block of JavaScript statements needed to pass the arguments sent by the JavaScript interpreter into the processErrors function, send an alert message with the agreements, return, and write the event handler that calls the processErrors function.
Custom Error