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

A throw expression in C# allows the `throw` keyword to be evaluated as an expression rather than a standalone statement. Introduced in C# 7.0, this feature enables developers to raise exceptions inline within specific expression contexts where a value is expected. Because a thrown exception unconditionally terminates the current execution path, a throw expression has no resulting value. Instead, the C# language specification dictates that a throw expression has an implicit conversion to any type. This satisfies the compiler's definite assignment and return type requirements within the expression tree.

## Syntactic Contexts

The C# language specification strictly limits throw expressions to specific grammatical contexts. You cannot use a throw expression arbitrarily in any place an expression is expected (such as a method argument or a standard binary operation). It is permitted exclusively in the following constructs:

**1. The Null-Coalescing Operator (`??`)**
The throw expression can serve as the right-hand operand of a null-coalescing operation. If the left-hand operand evaluates to `null`, the expression evaluates the right-hand operand, throwing the exception.

```csharp theme={"dark"}
string? input = null;
string value = input ?? throw new ArgumentNullException(nameof(input));
```

**2. The Conditional (Ternary) Operator (`?:`)**
A throw expression can be used as either the consequence (true) or alternative (false) branch of a conditional operator. The compiler infers the type of the entire ternary expression from the non-throwing branch.

```csharp theme={"dark"}
int age = -5;
int validAge = age >= 0 ? age : throw new ArgumentOutOfRangeException(nameof(age));
```

**3. Expression-Bodied Members and Functions (`=>`)**
Methods, properties, indexers, and constructors defined using expression-bodied syntax can consist entirely of a throw expression. Additionally, throw expressions can serve as the body for anonymous functions (lambdas) and local functions.

```csharp theme={"dark"}
using System;

public class Entity
{
    // Expression-bodied property
    public string Name => throw new NotImplementedException();

    // Expression-bodied method
    public int GetId() => throw new NotSupportedException();

    public void Evaluate()
    {
        // Lambda expression
        Func<int> getInt = () => throw new InvalidOperationException();

        // Local function
        void LocalFunc() => throw new Exception();
    }
}
```

**4. Switch Expressions**
Introduced in C# 8.0, switch expressions allow throw expressions within their switch arms. This is typically used in conjunction with the discard pattern (`_`) to handle non-exhaustive matches.

```csharp theme={"dark"}
using System;

int state = 3;
string stateName = state switch
{
    0 => "Idle",
    1 => "Active",
    _ => throw new ArgumentException("Invalid state", nameof(state))
};
```

## Compiler Evaluation Mechanics

When the C# compiler encounters a throw expression, it does not bypass type-checking. Instead, the C# specification explicitly defines that a throw expression possesses an implicit conversion to *every* type. Because the evaluation of a throw expression never successfully yields a value to the surrounding execution context (it is a non-returning operation), the type-checker actively applies this universal implicit conversion rule to satisfy the expression tree's requirements.

For example, in the ternary operation `age >= 0 ? age : throw new ArgumentOutOfRangeException()`, the compiler evaluates the true branch as type `System.Int32`. It evaluates the false branch as a throw expression. The type-checker then applies the implicit conversion from the throw expression to `System.Int32`, safely resolving the type of the entire ternary expression as `System.Int32` without generating a type mismatch error.

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