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

alorma

10/26/2020, 6:33 PM
why is not possible to do a
try
inside a @Composable block?
Copy code
@Composable
override fun build() {
   try {

   }
}
g

Grigorii Yurkov

10/26/2020, 7:09 PM
Composable
annotation is not just annotation, it forces compiler to make magic things under the hood. That's like you can't use
synchronized
block in suspend functions. And I think common ui don't need
try catch
and if it needs that means you are doing something wrong
a

alorma

10/26/2020, 7:10 PM
well, things crash, like firebase if is not initialized
and I'm doing a library that wrapps firebase...
j

Jeremy

10/26/2020, 7:13 PM
Stuff like that I think want to wrap in a
LaunchedTask
otherwise it will get executed multiple times
You should be able to use try/catch in a
LaunchedTask
j

jaqxues

10/26/2020, 7:14 PM
Well you should handle that in a viewmodel. Or have another architecture that tells the ui exactly how to look, without mixing any logic (no try catch) with ui code. That also simplifies the recomposition problem
1
a

alorma

10/26/2020, 7:16 PM
well yes
but the firebase user will live on a library, so i thougt tha tlibrary can handle itself if firebase auth is loaded or not
j

jim

10/26/2020, 9:47 PM
Long term, we do intend (or at least, would like) to eventually support try-catch blocks. The reason we don't support them yet is primarily due to prioritization of our engineering efforts. Supporting try-catch semantics is a lot of engineering work because the way Compose works with recomposition and parallelization, it is possible that a child is called in situations where the parent isn't on the call stack. But when an exception is thrown, you want the control flow to pass back through the parent, even if the parent was skipped or executed on a different thread. Getting this right requires a decent amount of engineering, and so we had to prioritize other things that were more critical to an initial stable release. Hopefully in a followup release, this can be supported.
🙏 5
5
m

Mark Murphy

10/26/2020, 10:32 PM
At the risk of asking a silly question... what's the problem with
try/catch
in a composable? I just tried it, and everything seemed to run correctly. My exception was caught, and the composable still rendered. I'm not doubting that there is a problem, but, what's the scenario that demonstrates it?
j

jim

10/26/2020, 10:33 PM
To reproduce, attempt calling a composable from within the try-catch block. We disallow that.
m

Mark Murphy

10/26/2020, 10:34 PM
ah, there we go -- thanks!
a

Abhishek Bansal

09/10/2023, 7:38 AM
I got a crash trying to render a large image. It’s not a OOM crash but
Canvas: trying to draw too large
exception. What is the way to handle such issues apart from reducing image size? because image size may not be in my control all the time.
m

Mark Murphy

09/10/2023, 3:55 PM
I would use something like telephoto.
💚 1
a

Abhishek Bansal

09/12/2023, 7:05 AM
Right, but unfortunately the crash is coming from inside another library.