https://kotlinlang.org logo
#compose
Title
# compose
a

amar_1995

11/21/2019, 11:37 AM
We cannot able to use
try-catch
inside composable function. I am getting the below error
Try catch is not supported around composable function invocations.
😄 2
k

kioba

11/21/2019, 11:41 AM
why do you want to throw?
a

amar_1995

11/21/2019, 12:04 PM
Just getting some error and I am trying to printstacktrace.😅
m

Michal Bacik

11/21/2019, 12:14 PM
He doesn't want to throw, but catch ;)
🤣 1
a

amar_1995

11/21/2019, 12:19 PM
Yes
I believed that we can able to use every functionality provided by kotlin.
l

Luca Nicoletti

11/21/2019, 1:28 PM
@Michal Bacik he can catch inside the function, without calling nested
@Composable
inside the try-catch
a

Adam Powell

11/21/2019, 2:06 PM
Thanks to the way Compose can restart further down the tree on recomposition if just one small part of the UI invalidates, the try/catch may not be part of the call stack when the error happens. Any time you call a composable function, think of it being a little bit like a
launch {}
.
We might wire this up to try to make it do something reasonable but it's not top priority at the moment
l

Luca Nicoletti

11/21/2019, 2:07 PM
I really don’t see the point why you should wrap a
@Composable
with a try-catch block
1
a

Adam Powell

11/21/2019, 2:08 PM
No? I'd love to be able to try a block and if anything goes wrong, transactionally roll back some part of it and then emit something else as part of the catch 🙂
l

Luca Nicoletti

11/21/2019, 2:08 PM
Well, you can try-catch inside a composable
a

Adam Powell

11/21/2019, 2:09 PM
Yes but that's not really exceptions, that's more like return value checking at each layer of the call stack
It unfortunately is a hole in our goal that any chunk of compose code is a candidate for refactor/extract method
One that can be worked around, but still
1004 Views