Is try/catch supported in `@Composables` now? I tr...
# compose
m
Is try/catch supported in
@Composables
now? I tried using
runCatching
and I got a weird error where the result was interpreted as a
T
(and not a
Result<T>
, see screenshot the debugger sees two types for the same lvalue ?!?). I ended up using try/catch and now it's all working but searching the backlog of this channel (https://kotlinlang.slack.com/archives/CJLTWPH7S/p1603737209131700), it looks like this might be dangerous?
j
It is safe to catch exceptions in a composable function, but not safe for a composable function to throw an exception. The compiler will usually give you an error if you try to wrap a composable function in a try-catch block because it indicates that you might have been expecting that the composable might throw.
m
Well then I'm definitely in a weird spot... Is there a reason
runCatching
would behave differently as
try/catch
?
j
Because the compiler doesn't know that
runCatching
catches exceptions.
m
Ah
But still, it should expect a
Result<T>
?
Looks like the compiler has "forced unwrapped" the Result but the downstream code doesn't know it or something like this
j
I wouldn't expect the type to be impacted, please file a bug with a minimal repro.
👍 1
m
(Fwiw it's compose desktop, not sure if it changes something)
j
That should not change anything, but the bug should be filed against upstream Compose (https://issuetracker.google.com/issues?q=componentid:612128) as failures in the type system would not be a desktop-specific issue.
👍 1
m
j
👍 Thanks!