TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
else clause in Kotlin defines the terminal, fallback execution branch for if and when conditional constructs. It executes exclusively when all preceding boolean conditions or branch matchers evaluate to false or fail to match the evaluation subject.
Because Kotlin treats if and when as expressions (constructs that evaluate to a value) rather than strictly as statements, the else clause plays a critical role in compiler-enforced exhaustiveness and type safety.
Mechanics in if Constructs
In an if construct, the else block is bound to the immediately preceding if or else if branch.
- Statement Context: If the
ifconstruct is used as a statement (its return value is ignored), theelseclause is optional. - Expression Context: If the construct is used as an expression (assigned to a variable, passed as an argument, or returned), the
elseclause is mandatory. The compiler requires a guaranteed return value for all possible execution paths.
Mechanics in when Constructs
In a when construct, the else clause functions as the default branch, denoted by the else -> syntax. The right-hand side of the -> operator must contain an expression or a block.
Strict Ordering Rule: The else -> branch must be the very last branch in the when block. If else -> is placed before any other matchers, the Kotlin compiler throws a strict error ('else' entry must be the last one in a when-expression).
- Exhaustiveness Requirement: The
elsebranch is mandatory whenever awhenconstruct must be exhaustive, unless the compiler can statically verify that all possible cases are explicitly covered. Exhaustiveness is strictly required in the following scenarios:- Expression Context: Whenever
whenis used as an expression, regardless of the subject type. - Statement Context: Since Kotlin 1.7.0, non-exhaustive
whenstatements forenumclasses,sealedclasses/interfaces, andBooleansubjects are enforced as a strict compiler error. If all possible values of these constrained types are not explicitly covered, theelsebranch is mandatory.
- Expression Context: Whenever
Block Evaluation and Return Types
When anelse clause contains a block with multiple statements, the evaluated result of the entire else branch is the value of the last expression within that block.
If the branches of an if or when expression return different types, the compiler resolves the overall expression to their closest common supertype.
- Implicit
Unit: If theelsebranch does not explicitly evaluate to a value, its return type is implicitlyUnit. If the correspondingifbranch returns a specific type (e.g.,Int), the common supertype resolves toAny. - The
NothingEdge Case: If theelsebranch throws an exception or calls a function that returnsNothing(such asTODO()), the compiler resolves the overall expression type strictly to the type of theifbranch. BecauseNothingis a bottom type (a subtype of all types in Kotlin), it does not force the common supertype toAny.
Master Kotlin with Deep Grasping Methodology!Learn More





