try, catch and finally
try runs risky code, catch handles an error and finally always runs, which is good for cleanup. throw creates your own errors.
Example
function parse(t) {
try { return JSON.parse(t); }
catch (e) { return "bad json"; }
finally { console.log("done"); }
}
console.log(parse("{}"), parse("{"));Exercise
Always runs.
try {} {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.