Skip to main content

How to Catch an Error from await with try...catch in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

When an awaited promise fails, await throws its error right there in your function, just like a normal error. Put the await inside a try block and the catch block gets that error, so the program can report it and carry on instead of crashing.

Syntax​

Syntax
try {
const <result> = await <promise>;
} catch (<err>) {
// handle <err>, for example with <err>.message
}
PartWhat it does
try { ... }Runs the code that might fail.
await <promise>Throws the promise's error inside try when the promise is rejected.
catch (<err>) { ... }Runs only when something in try threw, with the error in <err>.
<err>.messageThe error's text.

MDN's try...catch reference: "The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed."

The Example​

The example reads a file named notes.txt that doesn't exist. Reading it fails, and the catch block prints a friendly message with the reason.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js, in a folder without a notes.txt:

main.js, which loads readFile from node/promises and, inside an async main, awaits readFile('notes.txt') in a try block and prints Could not read the file: with err.message in the catch block.

main.js
const { readFile } = require('node:fs/promises');

async function main() {
try {
const text = await readFile('notes.txt', 'utf8');
console.log(text);
} catch (err) {
console.log(`Could not read the file: ${err.message}`);
}
}

main();
  • readFile returns a promise. The file doesn't exist, so the promise is rejected.
  • await turns that rejection into an error thrown inside try, so the console.log(text) line is skipped.
  • catch (err) receives the error, and err.message says what went wrong.

Step 2: Run It​

Terminal
node main.js
Output
Could not read the file: ENOENT: no such file or directory, open 'notes.txt'

A zsh terminal where node main.js prints Could not read the file: ENOENT: no such file or directory, open 'notes.txt'.

The program handled the failure and ended normally, with exit code 0. ENOENT is the operating system's code for "no such file". The command is the same in zsh, bash and PowerShell.

What Happens Without await​

Leave out await and try finishes before the promise fails, so catch never runs. I changed the line to const text = readFile('notes.txt', 'utf8'); and ran it again:

Output
Promise { <pending> }
node:internal/process/promises:324
triggerUncaughtException(err, true /* fromPromise */);
^

[Error: ENOENT: no such file or directory, open 'notes.txt'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'notes.txt'
}

Node.js v26.13.1

text held the promise, not the file's contents, and when the promise failed a moment later nothing was waiting for it, so Node.js crashed with exit code 1.

Common Mistakes​

MistakeWhat happensDo this instead
Calling a promise-returning function inside try without awaitThe error escapes catch and the program crashesawait it inside the try
Catching every error and saying nothingProblems disappear silentlyAt least print err.message
Using text after the try blocktext exists only inside tryUse it inside try, or declare it with let above

Troubleshooting​

SymptomLikely causeFix
A crash at node:internal/process/promises despite a try...catchThe promise wasn't awaitedAdd await
ENOENT: no such file or directoryThe path is wrong, or the program runs from another folderCheck the path and the folder you run node from

FAQ​

Can I leave out (err) when I don't need the error?​

Yes: catch { ... } is valid JavaScript and runs the block without giving you the error.

Does try...catch work with .then() chains too?​

Only with await. A plain .then() chain needs its own .catch() method instead.

Conclusion​

Wrap the await in try, handle the failure in catch and read err.message for the reason. The await is what makes it work: without it, the error arrives after try has already finished.

Additional Resources​