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

# C# Throw Statement

The `throw` statement explicitly signals the occurrence of an anomalous execution condition (an exception) at runtime. When executed, it immediately halts the normal sequential flow of the program and initiates the Common Language Runtime (CLR) two-pass exception handling process, ultimately transferring control to the nearest dynamically enclosing `catch` clause that matches the thrown exception type.

## Syntax

```csharp theme={"dark"}
throw [expression];
```

The `expression` must evaluate to an object that derives from the `System.Exception` base class. If the expression evaluates to `null`, the CLR automatically throws a `NullReferenceException` instead.

## Execution Mechanics

The .NET exception handling mechanism operates using a two-pass model:

1. **Evaluation:** The exception expression is evaluated to produce an exception object.
2. **Stack Trace Generation:** The CLR populates the `StackTrace` property of the exception object with the current execution point.
3. **Pass 1 (Searching):** The runtime traverses the call stack looking for a `try` block with a compatible `catch` clause. During this pass, any exception filters (`when` clauses) are evaluated.
4. **Pass 2 (Stack Unwinding):** Once a matching `catch` clause is identified, the runtime unwinds the stack from the point of the exception to the target `catch` block. This phase executes all `finally` blocks located in the scopes between the throw point and the catch point.
5. **Termination:** If the first pass reaches the top of the call stack without finding a matching `catch` block, the process is terminated via the unhandled exception mechanism, and the second pass does not occur.

## Forms of the Throw Statement

### 1. Throwing a New Exception

This is the standard form where a new exception instance is created and thrown.

```csharp theme={"dark"}
throw new ArgumentOutOfRangeException(nameof(index));
```

### 2. The Rethrow Statement (`throw;`)

When used inside a `catch` block, `throw` can be used without an expression. This is known as a rethrow.

```csharp theme={"dark"}
try
{
    // Code that may fault
}
catch (InvalidOperationException)
{
    throw; 
}
```

Mechanically, `throw;` propagates the caught exception while preserving its original `StackTrace`. This behaves differently than `throw ex;`, which re-throws the exception but mutates the exception object by resetting the `StackTrace` to the line containing the `throw ex;` statement.

### 3. Throw Expressions

Introduced in C# 7.0, `throw` can be evaluated as an expression rather than a statement. Because a throw expression never successfully returns a value, its return type is conceptually the bottom type, allowing it to implicitly convert to any type required by the surrounding context.

Throw expressions are permitted in the following syntactic contexts:

**Null-Coalescing Operator (`??`)**

```csharp theme={"dark"}
string validString = inputString ?? throw new ArgumentNullException(nameof(inputString));
```

**Conditional Operator (`?:`)**

```csharp theme={"dark"}
int result = isValid ? 100 : throw new InvalidOperationException();
```

**Expression-Bodied Members (`=>`)**

```csharp theme={"dark"}
public DateTime DateOfBirth => throw new NotImplementedException();
```

**Switch Expression Arms** (Introduced in C# 8.0)

```csharp theme={"dark"}
string state = input switch
{
    0 => "Idle",
    1 => "Active",
    _ => throw new ArgumentOutOfRangeException(nameof(input))
};
```

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