Another valuable tool in exception handling is the finally
keyword. You can add this keyword to the end of a try
/catch
block. After the try
/catch
block is executed, the finally
block is always executed, whether an error occurs and is caught or the try
block is fully executed.
Here’s an example of using a finally
block inside a web page:
function testTryCatch(value){
try {
if (value < 0){
throw "too small";
} else if (value > 10){
throw "too big";
}
your_code_here
} catch (err) {
console.log("The number was " + err);
} finally {
console.log("This is always written.");
}
}