Why error handling matters
Network requests fail. Servers go down. Users lose internet. APIs return unexpected data. If you don’t handle errors, your app shows a blank screen or crashes silently.try/catch/finally
The core pattern for handling errors in async code:- JavaScript
- Python
catch block.
Two types of fetch errors
This is where people get confused.fetch can fail in two different ways:
1. Network errors — fetch itself throws
2. HTTP errors — fetch succeeds but status is bad
Handling both types
User-friendly error messages
Don’t show raw error messages to users. Translate them into something helpful:In React
Re-throwing errors
Sometimes you want to handle an error and let the caller handle it too:Common mistakes
Swallowing errors silently
Swallowing errors silently
Not checking response.ok before parsing
Not checking response.ok before parsing
Using .catch() and try/catch together unnecessarily
Using .catch() and try/catch together unnecessarily
What’s next?
Error handling keeps your app stable. Now let’s make it feel fast with loading states — showing users what’s happening while requests are in flight.Loading states
Show users what’s happening during requests