Wrap code that may throw in try { … }; if anything inside throws, control jumps to catch (err) { … } with the thrown value; finally { … } always runs afterwards for cleanup. try/catch only catches errors thrown synchronously in that block, or from an awaited promise in an async function. Catch errors where you can do something useful (recover, add context, show UI), and rethrow the rest.
function parseConfig(text) {
try {
return JSON.parse(text);
} catch (err) {
if (err instanceof SyntaxError) {
console.warn('Invalid config, using defaults');
return {};
}
throw err; // not ours to handle
} finally {
console.log('parse attempted'); // always runs
}
}
Optional catch binding (ES2019) when you don't need the error: try { … } catch { … }.
async function loadProfile(id) {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`, { cause: res });
return await res.json(); // await here so a JSON error is caught too
} catch (err) {
throw new Error(`Could not load profile ${id}`, { cause: err });
}
}
What try/catch doesn't catch:
try {
setTimeout(() => { throw new Error('later'); }, 0); // runs after try has exited
} catch { /* never reached */ }
try {
fetch('/x').then((r) => r.json()); // promise not awaited → unhandled rejection
} catch { /* never reached */ }
try {
return promise; // in an async function: rejection escapes; use `return await promise`
} catch {}
Callbacks and un-awaited promises need their own handling (.catch(), try inside the callback, or the global unhandledrejection event).
catch {} hides bugs. If ignoring is intentional, comment why.instanceof or err.name, and rethrow unknown errors.try blocks small so you know which operation failed and don't accidentally catch bugs in unrelated code.cause instead of losing the original error.err is unknown; narrow it before reading .message.function f() {
try { return 'try'; }
finally { console.log('cleanup'); } // runs before the function actually returns
}
A return or throw inside finally overrides the try/catch result, which can silently discard an error. Avoid that.
Modern engines optimise functions containing try/catch well. Throwing is relatively costly (stack capture), so don't use exceptions for normal control flow in hot loops.
try/catch, and return await inside try.cause chaining.Spotted a mistake or a rights concern? Report an issue