← Back to Kotlin Course | Chapter 2: Variables & Types | Lesson 6 of 7

The Nothing Type

The Nothing type is Kotlin's way of marking code paths, like throwing an error, that never actually finish or return a normal value.
Syntax
kotlin
fun functionName(message: String): Nothing {
    throw SomeException(message)   // never returns normally
}

val value = nullable ?: throw SomeException()

What Nothing Represents

Nothing is a special type that has no values at all. It signals that a function will never return normally -- it either throws an exception or loops forever.

Example: What Nothing Represents

markup
// Define the function `fail` taking `message` and returning a `Nothing`
fun fail(message: String): Nothing {
    throw IllegalStateException(message)
}

// Entry point: execution of the program starts here
fun main() {
    try {
        fail("Something went wrong")
    } catch (e: IllegalStateException) {
        // Print "Caught: ${e.message}" to the console, with a trailing newline
        println("Caught: ${e.message}")
    }
}

Nothing as a Subtype of Everything

Because Nothing is a subtype of every other type in Kotlin, an expression of type Nothing can be used anywhere a value of any type is expected, letting the compiler treat both branches consistently.

Example: Nothing as a Subtype of Everything

markup
// Define the function `getOrDefault` taking `value` and returning an Int
fun getOrDefault(value: Int?): Int {
    // Return `value ?: throw IllegalArgumentException("value was null")` from this function
    return value ?: throw IllegalArgumentException("value was null")
}

// Entry point: execution of the program starts here
fun main() {
    // Print `getOrDefault(5)` to the console, with a trailing newline
    println(getOrDefault(5))
}

TODO() Returns Nothing

The standard library's TODO() function has a return type of Nothing and throws a NotImplementedError, letting you stub out a function body while still satisfying the compiler's type checks.

Example: TODO() Returns Nothing

markup
// Define the function `plannedFeature` with no parameters and returning an Int
fun plannedFeature(): Int {
    // Return `try {` from this function
    return try {
        TODO("implement this later")
    } catch (e: NotImplementedError) {
        -1
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Placeholder result: ${plannedFeature()}" to the console, with a trailing newline
    println("Placeholder result: ${plannedFeature()}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing Nothing with null or with Unit; Nothing means a function never completes normally, while Unit means it completes but returns no useful value.
  2. Not realizing that a function like TODO() returns Nothing, which is why it can be used anywhere a value is expected without a type error.
  3. Trying to create an instance of Nothing; it has no instances -- it exists only to describe code that never returns.
Chapter Summary
  • Nothing is a type with no instances, used to represent an expression that never completes normally.
  • Functions that always throw an exception, like ones ending in throw, have an inferred return type of Nothing.
  • Because Nothing is a subtype of every other type, it can be used anywhere any type is expected, such as after ?? in the Elvis operator.
  • TODO() from the standard library returns Nothing, letting you stub out unfinished code that still type-checks.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.