Error Handling
Error Handling
Section titled “Error Handling”Introduction
Section titled “Introduction”With async/await, error handling uses the familiar try/catch syntax instead of .catch() on promises.
Basic Try/Catch
Section titled “Basic Try/Catch”async function loadData() { try { const user = await fetchUser(1); const posts = await fetchPosts(user.id); const comments = await fetchComments(posts[0].id); return comments; } catch (error) { console.error('Something went wrong:', error); return []; // Default value on error }}Multiple Catch Blocks
Section titled “Multiple Catch Blocks”async function robustLoad() { try { return await fetch('/api/data').then(r => r.json()); } catch (error) { if (error.name === 'AbortError') { console.log('Request was cancelled'); return null; } if (error instanceof TypeError) { console.log('Network error — using cache'); return loadFromCache(); } // Generic error console.error('Unexpected error:', error); throw error; // Re-throw if we can't handle it }}Without try/catch
Section titled “Without try/catch”// .catch() also works with async/awaitasync function load() { const data = await fetch('/api/data') .then(r => r.json()) .catch(err => { console.error(err); return null; }); return data;}Summary
Section titled “Summary”- Use
try/catchfor sync-style error handling - Multiple catch blocks for different error types
- Can mix
.catch()withawaitfor hybrid approach - Always handle errors in async functions