> ## 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.

# Dart Catch Clause

A `catch` clause in Dart is an exception-handling construct used immediately following a `try` block to intercept and process objects thrown during execution. It prevents unhandled exceptions from terminating the current isolate and provides access to the thrown object and its execution context.

In Dart, because any non-null object can be thrown, the `catch` clause is designed to handle arbitrary types, though it is most commonly used with instances of `Exception` or `Error`.

## Syntax and Parameters

The `catch` clause accepts one or two parameters:

1. **Exception Object (`e`)**: The first parameter captures the thrown object. By default, its type is `Object`.
2. **Stack Trace (`s`)**: The optional second parameter captures an instance of `StackTrace`, representing the call stack at the exact point the exception was thrown.

```dart theme={"dark"}
try {
  // Executable code that may throw an object
} catch (e, s) {
  // e: The thrown object
  // s: The StackTrace
}
```

## Type-Specific Interception (`on` keyword)

To handle different exceptions polymorphically, Dart pairs the `catch` clause with the `on` keyword. The `on` keyword filters exceptions based on their runtime type.

* If you need to inspect the exception object, use `on Type catch (e)`.
* If you only need to handle the type but do not need the object itself, omit the `catch` clause entirely and use `on Type`.

```dart theme={"dark"}
try {
  // Executable code
} on FormatException catch (e, s) {
  // Intercepts only FormatException (and its subtypes)
  // Captures both the exception object and the stack trace
} on StateError {
  // Intercepts StateError (built-in from dart:core)
  // Discards the exception object and stack trace
} catch (e) {
  // Fallback: Intercepts any remaining thrown object
  // Captures only the exception object
}
```

## Evaluation Mechanics

* **Sequential Evaluation**: Multiple `on`/`catch` blocks are evaluated sequentially from top to bottom. The Dart runtime executes the first block where the thrown object's runtime type is a subtype of the type specified in the `on` clause.
* **Catch-All Behavior**: A standalone `catch` block (without an `on` modifier) acts as a catch-all. It is equivalent to `on Object catch (e)`. It must always be placed at the bottom of the handler chain to prevent unreachable code errors.
* **Propagation via `rethrow`**: Within a `catch` block, the `rethrow` keyword can be used to propagate the intercepted exception up the call stack. Unlike throwing a new exception or explicitly throwing the captured object (`throw e;`), `rethrow` preserves the original `StackTrace` of the exception.

```dart theme={"dark"}
try {
  // Executable code
} catch (e, s) {
  // Partial handling logic
  rethrow; // Propagates 'e' while maintaining the original 's'
}
```

<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 Dart 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>
