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

# Kotlin Throw Expression

In Kotlin, `throw` is an expression rather than a statement. It is used to explicitly raise an exception at runtime, terminating the normal execution of the current scope. Because it evaluates as an expression, it possesses a distinct type in the Kotlin type system, allowing it to be composed directly within other expressions.

## Syntax

The `throw` keyword must be followed by an expression that evaluates to an instance of a `Throwable` class. This can be a direct instantiation, a variable reference, or a function call that returns a `Throwable`.

```kotlin theme={"dark"}
// Direct instantiation
throw IllegalArgumentException("Invalid argument")

// Variable reference
val exception = IllegalStateException("Invalid state")
throw exception
```

## The `Nothing` Type

The evaluation type of a `throw` expression is `Nothing`. In Kotlin's type hierarchy, `Nothing` is a bottom type, meaning it is a subtype of every other type.

Because a `throw` expression never successfully evaluates to a value (it unconditionally transfers control flow to the nearest exception handler), the compiler allows it to be used in any context where a specific data type is expected.

## Expression Composition

The `Nothing` return type allows `throw` to be seamlessly integrated into expressions that require a unified return type, such as the Elvis operator (`?:`) or `when` expressions.

**With the Elvis Operator:**

```kotlin theme={"dark"}
val text: String? = null

// The compiler expects an Int on the right side of ?: because text?.length evaluates to Int?.
// 'throw' evaluates to Nothing, which acts as a valid subtype of Int in this context.
val length: Int = text?.length ?: throw IllegalArgumentException("Text cannot be null")
```

**Within a `when` Expression:**

```kotlin theme={"dark"}
val status: String = "UNKNOWN"

// The 'when' expression must evaluate to an Int.
// The 'else' branch evaluates to Nothing, satisfying the type checker.
val code: Int = when (status) {
    "SUCCESS" -> 200
    "ERROR" -> 500
    else -> throw IllegalStateException("Unrecognized status")
}
```

## Control Flow and Smart Casting

The Kotlin compiler performs control flow analysis around `throw` expressions.

1. **Unreachable Code:** Any code sequentially following a `throw` expression within the same block is marked as unreachable by the compiler.
2. **Smart Casting:** If a `throw` expression is used to guard against a specific state (such as nullability or type checking), the compiler applies smart casting to the checked variable for all subsequent execution paths within that scope.

```kotlin theme={"dark"}
fun process(data: Any?) {
    // Guard condition using throw
    if (data !is String) throw TypeCastException("Data must be a String")
    
    // Control flow analysis guarantees 'data' is smart cast to a non-nullable String here
    println(data.uppercase())
}
```

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