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

Nothing Type क्या है

Nothing type उन code paths को चिह्नित करने का Kotlin का तरीका है, जैसे कोई error फेंकना, जो असल में कभी खत्म नहीं होते या एक normal value return नहीं करते।
Syntax
kotlin
fun functionName(message: String): Nothing {
    throw SomeException(message)   // never returns normally
}

val value = nullable ?: throw SomeException()

What Nothing Represents

Nothing एक खास type है जिसकी बिल्कुल कोई values नहीं हैं। यह संकेत देता है कि कोई function कभी सामान्य रूप से return नहीं करेगा -- यह या तो एक exception फेंकता है या हमेशा के लिए loop करता है।

उदाहरण: 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

चूंकि Nothing Kotlin में हर दूसरे type का subtype है, Nothing type का एक expression कहीं भी उपयोग किया जा सकता है जहां किसी भी type की value की उम्मीद हो, compiler को दोनों branches को consistently treat करने देते हुए।

उदाहरण: 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

standard library के TODO() function का return type Nothing है और यह एक NotImplementedError फेंकता है, आपको compiler की type checks को satisfy करते हुए एक function body को stub out करने देते हुए।

उदाहरण: 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. #}
आम गलतियां
  1. Nothing को null या Unit के साथ confuse करना; Nothing का मतलब है कि कोई function कभी सामान्य रूप से पूरा नहीं होता, जबकि Unit का मतलब है कि यह पूरा होता है लेकिन कोई उपयोगी value return नहीं करता।
  2. यह न समझना कि TODO() जैसा कोई function Nothing return करता है, यही वजह है कि इसे बिना type error के कहीं भी उपयोग किया जा सकता है जहां एक value की उम्मीद हो।
  3. Nothing का एक instance बनाने की कोशिश करना; इसकी कोई instances नहीं हैं -- यह सिर्फ उस code को describe करने के लिए मौजूद है जो कभी return नहीं करता।
चैप्टर सारांश
  • Nothing बिना instances वाला एक type है, जो किसी ऐसे expression को represent करने के लिए उपयोग होता है जो कभी सामान्य रूप से पूरा नहीं होता।
  • जो functions हमेशा एक exception फेंकते हैं, जैसे throw से खत्म होने वाले, उनका inferred return type Nothing होता है।
  • चूंकि Nothing हर दूसरे type का subtype है, इसे कहीं भी उपयोग किया जा सकता है जहां कोई भी type की उम्मीद हो, जैसे Elvis operator में ?? के बाद।
  • standard library से TODO() Nothing return करता है, आपको unfinished code को stub out करने देते हुए जो फिर भी type-check करता है।
🔒

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.