Archive for June 12, 2007

JavaScript throw Statement

Posted in JavaScript, Software, Web, internet, programming on June 12, 2007 by Joey

The JavaScript throw statement is used to send an exception to a block of code that will handle the exception.

try
{
if(/^\d+$/.test(2))
document.write('Positive integer validated');
else
throw('Invalid');
}
catch(strErrorMsg)
{
document.write(strErrorMsg);
}

The preceding example defines a try block, in which a validation for a positive integer occurs. If the validation is successful, a success message is printed. If the validation is not successful, however, the throw statement is used to send the error message to the catch block.

Related articles:
JavaScript Try Catch Finally