try / catch / finally
JS · Error Handlingsyntax
try { ... }
catch (error) { ... }
finally { ... }example
function parseConfig(jsonString) {
try {
return JSON.parse(jsonString);
} catch (error) {
console.error("Invalid config JSON:", error.message);
return {};
} finally {
console.log("Config parsing attempted.");
}
}
const config = parseConfig('{"debug": true}');
console.log(config);output
"Config parsing attempted."
{ debug: true }Note finally always runs, whether the try succeeded or catch was triggered. It even runs if try or catch contains a return statement.