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

A `try-catch` construct in Kotlin is an expression used for exception handling to intercept and process objects that inherit from the `Throwable` class. Unlike Java, Kotlin does not distinguish between checked and unchecked exceptions; all exceptions in Kotlin are unchecked, meaning the compiler never enforces catching or declaring them via a `throws` clause.

## Syntax

The construct consists of a mandatory `try` block, one or more optional `catch` blocks, and an optional `finally` block. At least one `catch` or `finally` block must be present.

```kotlin theme={"dark"}
try {
    // Block of code where exceptions may be thrown
} catch (e: IllegalArgumentException) {
    // Handler for a specific exception type
} catch (e: Exception) {
    // Handler for a broader exception type
} finally {
    // Block executed unconditionally after try/catch completion
}
```

## Expression Evaluation

Because `try-catch` is an expression in Kotlin, it evaluates to a value that can be assigned to a variable or returned from a function.

* **Success Path:** If no exception is thrown, the evaluated value is the last expression inside the `try` block.
* **Exception Path:** If an exception is thrown and matched, the evaluated value is the last expression inside the corresponding `catch` block.
* **Finally Block:** The contents of the `finally` block do not alter the result of the expression.

```kotlin theme={"dark"}
val result: Int = try {
    Integer.parseInt("100") // Evaluates to 100
} catch (e: NumberFormatException) {
    0 // Evaluates to 0 if an exception occurs
}
```

## Catch Block Mechanics

* **Type Matching:** The `catch` block parameter requires an explicit type declaration. Kotlin uses type checking (`is` operator logic implicitly) to match the thrown exception against the declared parameter type.
* **Evaluation Order:** `catch` blocks are evaluated sequentially from top to bottom. The first block that matches the exception type (or a supertype of the exception) is executed.
* **Hierarchy Enforcement:** To prevent unreachable code, handlers for derived exception classes (subclasses) must precede handlers for their base classes (supertypes).

## The `finally` Block

The `finally` block executes after the `try` block and any executed `catch` blocks, regardless of whether an exception was thrown, caught, or left unhandled.

While `finally` does not affect the value of the `try-catch` expression, an explicit `return` statement inside a `finally` block will override the return value of the enclosing function. This behavior alters the control flow and is generally considered an anti-pattern.

## Type System Integration

At compile-time, the Kotlin type system computes the type of a `try-catch` expression by finding the common supertype of the `try` block and all `catch` blocks.

If a `catch` block terminates by explicitly throwing an exception using a `throw` expression, that specific `catch` block evaluates to Kotlin's `Nothing` type at compile-time. Because `Nothing` is a bottom type (a subtype of all other types in Kotlin), it does not alter the inferred type of the overall `try-catch` expression.

```kotlin theme={"dark"}
// The compiler infers 'value' as String.
// The try block evaluates to String, and the catch block evaluates to Nothing.
// The common supertype of String and Nothing is String.
val value: String = try {
    "Success"
} catch (e: Exception) {
    throw IllegalStateException(e) 
}
```

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