Javascript Reference
Categories: Operators

javascript Operators throw

@March 15, 2008, 1:22 a.m.
throw Firefox/Netscape/NN 6 IE 5 ECMA 3  

  

Triggers an exception condition, passing a value along with the exception. Although the value you pass can be a simple string, ideally you should pass an instance of the JavaScript Error object filled with sufficient information for a catch statement to act intelligently on the error. A throw statement must be enclosed in the try portion of a try-catch construction.

 
Example
 
function processNumber(inputField) {
    try {
        var inpVal = parseInt(inputField.value, 10);
        if (isNaN(inpVal)) {
            var msg = "Please enter a number only.";
            var err = new Error(msg);
            if (!err.message) {
                err.message = msg;
            }
            throw err;
        }
        // process number
    }
    catch (e) {
        alert(e.message);
        inputField.focus( );
        inputField.select( );
    }
}

Powered by Linode.