Default for throw or null


Just for think about

Syntax sugar for constructions like

try {
    someWork() ?: someDefaultWork()
} catch (e: Exception) {


someWork(key) ?!: someDefaultWork()

Just for think about


Thought about it and don’t like it. You are actually proposing that all expressions be suppressed for a statement based on 1 character added to the statement.

I note that, assuming we don’t expect exceptions in someDefaultWork, your original code can be done as:

try { someWork() }
catch (_: Exception) { null }
?: someDefaultWork()

It would be trivial to write a function for suppressing exceptions and turning them into null so that it could be coded something like this:

nullifyException { someWork() } ?: someDefaultWork()

This would be the function which is not something I would want in the std library:

inline fun <T> nullifyException(block: () -> T)
    = try { block() } catch(_:Exception) { null }


I did for me

 tryor ({ some() }, { default() } )


Note that the second lambda can go outside the parentheses. Since that is the case I would recommend reversing the order so that it is:


I would have a version that takes a lambda default and one that accepts a value.


Oh, nice!

Try { getLotteryNumbers() }.getOrElse { ex: Throwable -> emptyList() }