> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Try-Catch-Finally

The `try...catch...finally` statement is a synchronous control flow construct used to handle runtime exceptions. It allows developers to execute potentially error-yielding code, intercept thrown exceptions to prevent program termination, and execute guaranteed cleanup operations regardless of the execution outcome.

```typescript theme={"dark"}
try {
  // Statements that may throw an exception
} catch (error) {
  // Statements executed if an exception is thrown
} finally {
  // Statements executed unconditionally after try/catch
}
```

## Execution Mechanics

* **`try` Block:** Defines the lexical scope for exception monitoring. If an exception is thrown (via the `throw` keyword or an internal runtime error), execution immediately halts, and control flow transfers to the `catch` block.
* **`catch` Block:** Defines the exception handler. It binds the thrown exception to a local variable. If no exception is thrown in the `try` block, the `catch` block is skipped.
* **`finally` Block:** Defines the termination clause. It executes after the `try` and `catch` blocks complete. The `finally` block is guaranteed to execute even if the `try` or `catch` blocks contain `return`, `continue`, `break`, or `throw` statements.

## TypeScript-Specific Typing Rules

TypeScript introduces strict typing rules for the `catch` clause variable that differ from standard JavaScript:

1. **No Explicit Type Annotations:** You cannot explicitly annotate the type of the catch variable. Writing `catch (error: Error)` results in compiler error `TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified`.
2. **The `unknown` Type:** By default (when `useUnknownInCatchVariables` is enabled in `tsconfig.json`, which is standard in modern TypeScript), the catch variable is implicitly typed as `unknown`. In older configurations, it defaulted to `any`.
3. **Type Narrowing:** Because the catch variable is `unknown`, TypeScript enforces type safety by requiring type guards (such as `instanceof` or `typeof`) to narrow the type before accessing its properties.

## Syntax Visualization with Type Narrowing

```typescript theme={"dark"}
try {
  throw new Error("Execution failed");
} catch (error: unknown) { // Explicit 'unknown' is allowed, but implicit is standard
  
  // Type narrowing is required to access properties safely
  if (error instanceof Error) {
    // TypeScript narrows 'error' to type 'Error'
    console.error(error.message);
  } else if (typeof error === "string") {
    // TypeScript narrows 'error' to type 'string'
    console.error(error.toUpperCase());
  } else {
    // Fallback for non-standard exceptions (e.g., throw 42, throw null)
    console.error("An unexpected error type was thrown");
  }

} finally {
  // Executes unconditionally
  console.log("Execution finalized");
}
```

## Omission of Blocks

The construct requires the `try` block and at least one of the subsequent blocks. It can be structured as:

* `try...catch`
* `try...finally`
* `try...catch...finally`

If `try...finally` is used without a `catch` block, the `finally` block will execute, but the exception will remain unhandled and will continue to propagate up the call stack immediately after the `finally` block completes.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
