why is not possible to do a `try` inside a @Compos...
# compose
a
why is not possible to do a
try
inside a @Composable block?
Copy code
@Composable
override fun build() {
   try {

   }
}
g
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
well, things crash, like firebase if is not initialized
and I'm doing a library that wrapps firebase...
j
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
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
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
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
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
To reproduce, attempt calling a composable from within the try-catch block. We disallow that.
m
ah, there we go -- thanks!
a
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
I would use something like telephoto.
💚 1
a
Right, but unfortunately the crash is coming from inside another library.