An exception is the way a program signals that something went wrong while it was running, such as dividing by zero, opening a file that does not exist, or losing a network connection. Instead of quietly producing garbage, the program raises an exception to say this is not something I can handle right now. That signal interrupts the normal flow and travels upward until some part of your code catches it and decides what to do. Handled well, exceptions let a program recover gracefully; ignored, they crash it. Here is what an exception is, how handling works, and how it differs from a syntax error.
How an exception works
While a program runs, it follows your instructions in order. When it hits a situation it cannot proceed through, like reading a missing file, it raises, or throws, an exception. At that point normal execution stops and the program looks for a handler.
The exception travels up through the chain of function calls, the call stack, looking for code that has volunteered to deal with it. If it finds one, that handler runs and the program can continue. If it finds none, the exception reaches the top, the program stops, and you see an error report describing what happened and where.
This unwinding is closely tied to how the program tracks running functions, which our explainer on what a stack is covers, since the stack trace you read is that chain in reverse.
Handling exceptions
The standard tool is a try-and-catch structure, which lets you attempt risky work and respond if it fails rather than letting the program die.
| Part |
Role |
| Try |
Wraps the code that might fail |
| Catch |
Runs if an exception is raised, letting you recover |
| Finally |
Runs either way, often to clean up resources |
| Throw |
Raises an exception yourself when something is wrong |
The goal is graceful recovery. A network call might fail, so you catch the exception and retry or show a friendly message. A missing setting might throw, so you fall back to a default. Handling turns a hard crash into a manageable bump.
Exception vs syntax error
People mix these up, but they happen at different times. A syntax error is caught before the program runs, when the language reads your code and finds it malformed. An exception happens during execution, when the code is valid but the situation is not.
- A syntax error means the language cannot even read your code; nothing runs until you fix it.
- An exception means the code ran but hit a problem it could not proceed through.
- The fix differs too. You correct syntax in the editor; you handle exceptions with try-and-catch logic.
Our guide to what a syntax error is covers the before-it-runs side in detail, so the two together give you the full picture of what can go wrong.
Common mistakes
- Swallowing exceptions silently. Catching an exception and doing nothing hides the real problem and makes bugs invisible.
- Catching everything. A broad catch-all can mask unrelated failures; catch the specific problems you expect.
- Using exceptions for normal flow. They are for unexpected problems, not for ordinary branching you could check directly.
- Ignoring cleanup. Use a finally block or equivalent to release files and connections even when something fails.
FAQ
What is the difference between an error and an exception?
The words overlap, but an exception specifically refers to a runtime problem the program signals and that you can often catch and handle, rather than a flaw the language refuses to run.
How do I handle an exception?
Wrap the risky code in a try block and add a catch block that runs if something goes wrong, letting you recover, retry, or report instead of crashing.
Should I catch every exception?
No. Catch the specific problems you expect and can handle. A blanket catch can hide unrelated bugs and make failures harder to diagnose.
Is an exception the same as a syntax error?
No. A syntax error stops the program before it runs; an exception happens during execution when valid code meets a problem it cannot proceed through.
Where to go next
See how the call stack unwinds in What Is a Stack in 2026, learn the before-run failures in What Is a Syntax Error in 2026, and read messages confidently with How to Read Error Messages in 2026.