2. Exception handling

Created Saturday 14 June 2014

try {
// code in this section usually executes normally, but if an exception happens, then execution will jump to the nearest catch block
} catch (e) {
// in this section, the statements are only executed if the try section throws an exception
} finally {
// statements in this section are always executed, even if there were no problems in the try section
}
var errCode = 0;
errCode = function_a(a, b, c);
if (errCode == -1)
handleError();
else if (errCode == -2)
handleAnotherError();
else if (errCode == -3)
handleYetAnotherError();
Etc...
try {
function_a(a, b, c);
function_b(d, e, f);
function_c(g);
} catch (e) {
handleError();
} finally {
clean_up();
}
function getName() {
var sName = prompt("Enter a name:", "");
if (!sName || sName.length == 0)
throw new Error("Name was not in a permitted form!");
alert("You entered: " + sName);
}
function testException() {
try {
getName();
} catch (e) {
alert(e.name + ": " + e.message);
}
}
window.onload = testException;



Backlinks: