Handling exceptions
Objectives
- use try, throw, and catch
- use console.assert() to catch errors
Try, throw, catch
- You can check for exceptions using the "try/catch" structure.
- You can throw an exception in code using the "throw" keyword.
- If an exception is thrown in a function and is not inside a "try" block,
then the rest of the code in the function does not execute.
- Any code that might cause an exception should be in a "try" code block.
- A "catch" code block follows the "try" block.
- If an exception is encountered in the "try" block, execution immediately jumps to the "catch" block.
- Keep in mind that any code in the "try" block following an exception is not executed.
- An optional "finally" block can follow the "catch" block. The "finally" block
code will run whether there was an error or not.
// example of code that throws exceptions
function add(a, b) {
if (arguments.length != 2) {
throw "Invalid number of arguments";
}
if (typeof a != 'number' || typeof b != 'number') {
throw "Illegal argument exception";
}
if (!isFinite(a) || !isFinite(b)) {
throw "Illegal number exception";
}
return a + b;
}
// this will display: Invalid number of arguments
try {
console.log(add());
} catch (err) {
console.log(err);
}
// this will display: Illegal argument exception
try {
console.log(add("1", "2"));
} catch (err) {
console.log(err);
}
// this will display: 7
try {
console.log(add(3, 4));
} catch (err) {
console.log(err);
}
// this will display: Invalid number of arguments
try {
console.log(add(1,2,3));
} catch (err) {
console.log(err);
}
// this will display: Illegal number exception
try {
console.log(add(5/0,3));
} catch (err) {
console.log(err);
}
// this will display: Illegal argument exception
try {
console.log(add(5,"2"));
} catch (err) {
console.log(err);
}
Using assert
- Note: console.assert should work well on Chrome, but may not be supported (or
supported well) in other browsers
- console.assert() is used for catching internal logic errors
- console.assert() should not be used for validating user input
- console.assert(expression): will cause an error message to
print on the console if expression evaluates to false
- console.assert(expression, message): will cause
an error message to print on the console if expression evaluates
to false; the message will be included with the error message
// example of code that throws exceptions
function add(a, b) {
console.assert(arguments.length==2, "Invalid number of arguments");
console.assert(typeof a == 'number' && typeof b == 'number', "Illegal argument exception");
console.assert(isFinite(a) && isFinite(b), "Illegal number exception");
return a + b;
}
console.log(add()); // displays: Invalid number of arguments
console.log(add("1", "2")); // displays: Illegal argument exception
console.log(add(3, 4)); // displays: 7
console.log(add(1,2,3)); // displays: Invalid number of arguments
console.log(add(5/0,3)); // displays: Illegal number exception
console.log(add(5,"2")); // displays: Illegal argument exception