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

In Kotlin, `try` is an expression rather than a statement, meaning it evaluates to a value that can be assigned to a variable, passed as an argument, or returned from a function. The evaluated result of the expression is determined by the last expression of the executed block.

```kotlin theme={"dark"}
val result = try {
    // Execution block
    1
} catch (e: IllegalArgumentException) {
    // Exception handling block
    2
} finally {
    // Cleanup block
    3
}
```

## Evaluation Mechanics

The value yielded by the `try` expression depends strictly on the execution path:

1. **Successful Execution:** If no exception is thrown within the `try` block, the expression evaluates to the value of the last expression inside the `try` block (`1` in the syntax above).
2. **Caught Exception:** If an exception is thrown and matches a `catch` block, the expression evaluates to the value of the last expression inside that specific `catch` block (`2`).
3. **Uncaught Exception:** If an exception is thrown but does not match any `catch` block, the exception propagates up the call stack. In this scenario, the execution results in abrupt completion. The static type of the `try` expression remains unchanged (determined at compile-time by the `try` and `catch` blocks), but no value is yielded at runtime.

## Type Inference

The Kotlin compiler determines the static type of the `try` expression by finding the least upper bound (the closest common supertype) of the types returned by the `try` block and all `catch` blocks.

```kotlin theme={"dark"}
val result = try {
    "Success" // Type: String
} catch (e: Exception) {
    42        // Type: Int
}
// The inferred type of 'result' is 'Any', as it is the common supertype of String and Int.
```

To maintain strict typing, the last expressions in both the `try` and `catch` blocks should ideally evaluate to the same type or a shared specific interface/superclass.

## The `finally` Block

The `finally` block is optional and executes after the `try` and `catch` blocks complete, regardless of whether an exception was thrown.

Crucially, the `finally` block **does not affect the evaluated result** of the `try` expression. Even if the `finally` block contains a returnable value or expression, it is ignored by the assignment.

```kotlin theme={"dark"}
val result: Int = try {
    10
} catch (e: Exception) {
    20
} finally {
    30 // This value is executed but discarded. It does not become the value of 'result'.
}
// 'result' will be 10.
```

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