Skip to main content

try Expression in Kotlin

3 min read
Share:
On this page (12sections)

Introduction

try Expression is a fundamental concept every Kotlin developer should understand. Exceptions signal that something went wrong at runtime. Kotlin treats checked exceptions differently from Java, encouraging explicit error handling where it matters.

In Kotlin, try-catch can be used as an expression that returns a value. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • In Kotlin, try-catch can be used as an expression that returns a value.
  • The last expression in try or catch becomes the result.
  • This reduces the need for temporary variables.

Syntax

val result = try { a / b } catch (e: Exception) { 0 }

try Expression in Kotlin Example Program in Kotlin

fun main(args: Array<String>) {
    fun divide(a: Int, b: Int): Int {
        return try {
            a / b
        } catch (e: ArithmeticException) {
            0
        }
    }
    println("10 / 2 = ${divide(10, 2)}")
    println("10 / 0 = ${divide(10, 0)}")
}

Sample Output

10 / 2 = 5
10 / 0 = 0

When to use

Use try/catch when calling code that can fail — file access, network calls, parsing — and you need a controlled recovery path.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. The println("10 / 2 = ${divide(10, 2)}") statement writes a line to the console — this produces part of the sample output below.

  3. The println("10 / 0 = ${divide(10, 0)}") statement writes a line to the console — this produces part of the sample output below.

  4. In Kotlin, try-catch can be used as an expression that returns a value.

  5. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Understand the core idea: in Kotlin, try-catch can be used as an expression that returns a value.
  • Prefer readable names and small functions so examples map directly to real projects.
  • Run and modify the example — change values and observe how the output changes.

Common Mistakes

  • Skipping the example and only reading the definition — hands-on practice cements the concept.
  • Copying syntax without understanding nullable vs non-nullable types or scope rules.
  • Ignoring compiler warnings that often point to safer alternatives.

Key Points

  • In Kotlin, try-catch can be used as an expression that returns a value.
  • The last expression in try or catch becomes the result.
  • This reduces the need for temporary variables.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is try Expression in Kotlin?
In Kotlin, try-catch can be used as an expression that returns a value.
When should I use try Expression?
Use try/catch when calling code that can fail — file access, network calls, parsing — and you need a controlled recovery path.
How is try Expression different from Java?
This reduces the need for temporary variables.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials