orThrow

fun <T> T?.orThrow(exception: () -> Exception): T

Returns the encapsulated value if it's not null, otherwise throws the specified exception. This is an alternative to the !! operator, allowing for a more descriptive exception to be thrown when a value is unexpectedly null.

Return

The non-null encapsulated value.

Parameters

exception

The exception to be thrown if the value is null.

Type Parameters

T

The type of the encapsulated value.

Throws

if the value is null.