AJAX - The Complete Reference

(avery) #1

582 Part IV: Appendixes


You can invoke exceptions directly using throw.

throw: value;

The value can be any value, but is generally an Error instance.
Exceptions can be handled with the common try/catch/finally block structure:

try {
statementsToTry
} catch ( e ) {
catchStatements
} finally {
finallyStatements
}

The try block must be followed by either exactly one catch block or one finally
block (or one of both). When an exception occurs in the catch block, the exception is placed
in e and the catch block is executed. The finally block executes unconditionally after
try/catch. We show a brief example that should be familiar to Ajax aficionados.

function createXHR()
{
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
return null;
}

Regular Expressions


JavaScript supports regular expressions, which are often used for filtering and validating
user input. A few examples are shown in Table A-20 to familiarize you with their format.

Exception Description
Error Generic exception.
EvalError Thrown when eval() is used incorrectly.

RangeError Thrown when a number exceeds the maximum allowable range.
ReferenceError Thrown on the rare occasion that an invalid reference is used.
SyntaxError Thrown when some sort of syntax error has occurred at runtime. Note
that “real” JavaScript syntax errors are not catchable.
TypeError Thrown when an operand has an unexpected type.

URIError Thrown when one of Global’s URI-related functions is used incorrectly.

TABLE A-19 JavaScript Exceptions
Free download pdf