Hullaballoonatic
06/14/2019, 9:41 PMtry {
...
} catch { e ->
...
}
try foo() catch { bar(it) }
try foo() catch bar()
or as @Marc Knaup suggested:
try foo() else bar()
instead of catch
in the event you are ignoring the throwableMarc Knaup
06/14/2019, 9:47 PMException
to Throwable
I guess.Hullaballoonatic
06/14/2019, 9:48 PMHullaballoonatic
06/14/2019, 9:48 PMHullaballoonatic
06/14/2019, 9:49 PMMarc Knaup
06/14/2019, 9:51 PMcatch
. That could imply that you can put any expression to catch
.
Why the {}
around the 3rd example?Marc Knaup
06/14/2019, 9:51 PMMarc Knaup
06/14/2019, 9:52 PMHullaballoonatic
06/14/2019, 9:52 PMMarc Knaup
06/14/2019, 9:52 PMval foo = try something() catch null
😄Marc Knaup
06/14/2019, 9:53 PMif
& else
val foo = if (something()) a else b
It just has an additional condition.Marc Knaup
06/14/2019, 9:54 PMcatch null
Hullaballoonatic
06/14/2019, 9:55 PMHullaballoonatic
06/14/2019, 9:55 PMif else
expressionMarc Knaup
06/14/2019, 9:55 PMval foo = try something() else null
or using lambda
val foo = try something() else { it.message }
Hullaballoonatic
06/14/2019, 9:55 PMHullaballoonatic
06/14/2019, 9:56 PMHullaballoonatic
06/14/2019, 9:56 PMMarc Knaup
06/14/2019, 9:57 PMThrowable
which isn't good 🤔Hullaballoonatic
06/14/2019, 9:58 PMtry foo() catch(e: Exception) bar()
Hullaballoonatic
06/14/2019, 9:58 PMif (someBool) foo() else bar()
Marc Knaup
06/14/2019, 9:59 PMMarc Knaup
06/14/2019, 9:59 PMelse
could imply Exception
for cases where you don't care about the kind of exception - but catching Throwable
is overkill (as it is usually)Hullaballoonatic
06/14/2019, 9:59 PMMarc Knaup
06/14/2019, 9:59 PMcatch
always with type, else
without and implies Exception
Marc Knaup
06/14/2019, 10:07 PMval currency = try Currency.getInstance(code) else null
Marc Knaup
06/14/2019, 10:08 PMval currency = runCatching { Currency.getInstance(code) }.getOrNull()
which feels weirdMarc Knaup
06/14/2019, 10:08 PMError
🤔Hullaballoonatic
06/14/2019, 10:12 PMorangy
on error resume next
Hullaballoonatic
06/14/2019, 10:32 PMr4zzz4k
06/14/2019, 10:48 PMonErrorResumeNext
operator, but never managed to understand why. Now I see that was from my childhood ramblings with VB 😁r4zzz4k
06/14/2019, 10:57 PMDico
06/15/2019, 6:03 AMlittlelightcz
06/15/2019, 8:41 AMrunCatching
for now 🙂