
No application runs without errors. Networks drop, users type unexpected input, files go missing and services time out. What separates a fragile program from a robust one is not the absence of errors but how it reacts to them. Graceful error handling means your application detects a problem, protects the user's data, explains it in plain language and leaves enough information for you to fix the cause.
Early languages such as C reported failures through return values like -1 or NULL, and forgotten checks caused countless bugs. Languages like C++, Java, Python and JavaScript introduced exceptions, which interrupt normal flow and travel up the call stack until some code handles them. Go returns errors as ordinary values, and Rust uses a Result type the compiler forces you to handle. All aim to make failures visible instead of silent.
Start by separating two kinds of problems:
So should you wrap everything in try/catch? No. Handle what you can do something useful about and let real bugs surface where you will notice them.
In Python, a handler looks like this: try: followed by the risky code, then except FileNotFoundError: with the recovery. In JavaScript you write try { ... } catch (err) { ... }, and for promises you use await inside try or add .catch(). Always catch the narrowest error type you expect. A bare except: also hides misspelled variable names and even the user pressing Ctrl+C.
Keep the try block small, ideally just the line that can fail, so you know exactly which operation went wrong.
The worst error handler is an empty one. Code such as catch (err) {} makes a problem disappear without a trace. Every handler should do at least one of these things:
When you re-raise, keep the original cause. Python supports raise ConfigError("Cannot load settings") from err, and JavaScript lets you write new Error("Cannot load settings", { cause: err }). The full chain then appears in your logs.
Users should never see a raw stack trace: it confuses them and can reveal file paths or database details useful to attackers. A good error message says what happened, whether their data is safe and what to do next: "We could not save your changes because the connection was lost. Your draft is kept on this device. Please try again." Avoid blaming the user and vague text with no next step.
In web services, return the correct HTTP status code, such as 400 for invalid input, 404 for a missing resource and 500 for an unexpected server failure, with a short, consistent error body. If you are building one, our guide to a REST API with Node.js shows where such responses fit.
Record the time, the severity level, the error type and message, the stack trace and useful context, such as the request or user ID. Standard levels are DEBUG, INFO, WARNING, ERROR and CRITICAL. Use a real logging library, such as Python's logging module or Pino for Node.js, rather than scattered print statements. Many teams write structured logs in JSON so tools can search them, and services such as Sentry group repeated crashes and alert you.
Never log passwords, card numbers, access tokens or other personal data. Logs are kept for months, and privacy laws such as the GDPR apply to them too. Good logs also make it much easier to debug code like a professional.
Temporary failures often succeed on a second attempt, but retrying instantly and forever overloads a struggling server. Use exponential backoff: wait one second, then two, then four, add a little random delay, and stop after a few attempts. Set timeouts on every network call.
Release resources even when something fails. Use finally blocks or Python's with statement to close files, database connections and locks.
Validate input at the edges, fail early with a clear message and handle errors at the level that knows what to do. A global handler at the top, such as error middleware in a web framework, can catch anything unexpected, log it and show a friendly page. Failures then become small, understandable events instead of mysterious crashes.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions