Suggest an editImprove this articleRefine the answer for “What is an exception”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**An exception is an event that occurs while the program is running: an error happened and the normal flow of the code is interrupted.** The JavaScript engine throws an error object, then looks for the nearest `try...catch` block that will catch this exception. Do not confuse the two notions: `Error` is an object holding data about what went wrong, while an exception is the very moment that object is thrown. Exceptions come in two kinds: automatic ones, thrown by the engine itself (for example a `ReferenceError` on an undeclared variable), and user defined ones, where the developer writes `throw new Error(...)`. If an exception is not caught, it bubbles up the call stack and terminates execution with an error. ```javascript try { throw new Error("Network error"); } catch (error) { console.log("Caught exception:", error.message); } ``` **Key point:** Error is the data, an exception is the event of throwing it; an uncaught exception bubbles up the stack and stops the program.Shown above the full answer for quick recall.Answer (EN)Image**An exception is an event that occurs while the program is running, when an error happens and the normal execution of the code is interrupted.** In other words, an exception is a "signal" about an error situation that the JavaScript engine throws (`throw`) and that you can catch (`catch`). ## Theory ### TL;DR - An exception is the moment an error is thrown, while `Error` is the object holding information about what went wrong. - An exception interrupts the normal flow of execution and starts the search for the nearest `try...catch`. - The mechanism rests on four keywords: `try`, `throw`, `catch`, `finally`. - Exceptions are either automatic (thrown by the engine) or user defined (thrown by the developer with `throw`). - If nobody catches an exception, it bubbles up the call stack and the program terminates with an error. ### Quick example ```javascript function divide(a, b) { if (b === 0) { throw new Error("Division by zero is not allowed"); } return a / b; } try { divide(5, 0); // here the exception is thrown } catch (err) { console.log("Caught exception:", err.message); } ``` ### Definition **An exception** is an **event** that arises **while the program is running**, when an error happens and **the normal execution of the code is interrupted**. Put differently, **an exception is a "signal" about an error situation** that the JavaScript engine throws (`throw`) and that can be caught (`catch`). ### The difference between error and exception - **Error** is an **object** that carries information about what went wrong. - **Exception** is the **moment** when that error object **was thrown** during execution. > Put differently: > Error is the **data**, > Exception is the **event of throwing that data**. ### How an exception actually arises Let us walk through the example above step by step: 1. The `divide` function runs. 2. The condition `b === 0` is true, and the engine **throws an error object**. 3. That event is the **exception**. 4. JavaScript starts looking for the nearest `try...catch`. 5. The `catch` block **intercepts** the exception, and the program keeps running. ### The exception mechanism: try / throw / catch / finally | Keyword | What it does | | --- | --- | | `try` | The block where an error may happen | | `throw` | Throws an exception | | `catch` | Intercepts the exception and handles it | | `finally` | Always runs, even if there was an error | Example: ```javascript try { throw new Error("Network error"); } catch (error) { console.log("Caught exception:", error.message); } finally { console.log("This code always runs"); } ``` ### Two kinds of exceptions 1. **Automatic (thrown by the engine).** They appear when the engine itself detects an error: ```javascript console.log(a); // ReferenceError, the exception is thrown automatically ``` 2. **User defined (explicit).** When the programmer throws the exception themselves: ```javascript throw new Error("Something went wrong"); ``` ### What happens if an exception is not caught If an exception is **not intercepted** (there is no `catch` block), it bubbles up the call stack. If in the end nobody caught it, the program **terminates with an error**: ```javascript function a() { b(); } function b() { throw new Error("Failure"); } a(); // the error will terminate execution ``` In the browser such an exception ends up in the console and, if you want, in the global `window.onerror` handler; in Node.js the `uncaughtException` event terminates the process by default. ### Common mistakes - **Confusing `Error` and an exception.** Creating `new Error("...")` on its own interrupts nothing: it becomes an exception only after `throw`. - **An empty `catch`.** A `catch {}` block with no handling hides the problem: the program runs on in a broken state and you can no longer see the cause. - **Expecting `try...catch` to catch an asynchronous error.** An exception thrown inside a `setTimeout` callback or in an unhandled promise will not reach the surrounding `try...catch`; promises need `await` inside `try`, or `.catch()`. - **Throwing something that is not an error object.** `throw "text"` is technically allowed, but then you have neither `message` nor `stack`; always throw `new Error(...)` or a subclass of it. - **Forgetting that `finally` runs even after `return`.** A `return` inside `finally` overwrites the value returned from `try` and can silently swallow the exception.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.