Pawel
03/16/2020, 6:19 PMvar a: Int? = null
try {
a = doSthToGetA()
useA(a)
} catch(ex: AnyExc) {
doSthWithA(a)
}
SackCastellon
03/16/2020, 6:25 PMdoSthToGetA()
, useA(a)
or both?
And does doSthWithA(a)
expect a nullable type?
Do you care about the exception thrown or you just want to know whether an exception was thrown or not?Pawel
03/16/2020, 6:26 PMa
is null in catch
blockSackCastellon
03/16/2020, 6:43 PMrunCatching
but it doesn't make thinks better.
var a: Int? = null
runCatching { a = doSthToGetA().also(::useA) }.onFailure { doSthWithA(a) }
This could be an alternative, however in this case a
is only assigned if doSthToGetA
and useA
don't throw an exception.