Ask any question about JavaScript here... and get an instant response.
Why does my try-catch block not catch errors from an async function call?
Asked on Nov 02, 2025
Answer
A try-catch block does not catch errors from an async function call because the async function returns a Promise. To handle errors from async functions, you should use the "catch" method on the Promise or use "await" inside a try-catch block.
async function fetchData() {
throw new Error("Network error");
}
// Using await inside try-catch
async function getData() {
try {
await fetchData();
} catch (error) {
console.error("Caught an error:", error.message);
}
}
getData();Additional Comment:
✅ Answered with JavaScript best practices.- The "fetchData" function is an async function that throws an error.
- In "getData", "await" is used inside a try-catch block to handle the error.
- Without "await", the error would not be caught by the try-catch block.
- Alternatively, you can handle errors using the "catch" method on the Promise returned by the async function.
Recommended Links:
