Try-Catch vs. runCatching()

  Рет қаралды 4,796

Dave Leeds

Dave Leeds

Күн бұрын

Пікірлер: 25
@Drakind
@Drakind 11 ай бұрын
For me the greatest flaw with runCatching is its compatible/incompatibility with coroutines. As mentioned in the video it catches everything, including coroutines Cancellation Exceptions. Which can easily cause unforeseen behavior. There are ways to handle it, but I find it easier to have correct flow when using try catch instead of runCatching.
@typealias
@typealias 11 ай бұрын
Yeah, that makes sense - better to carefully catch the exact right exceptions in that case. It'd be cool if we could do something like `runCatching { ... }` - but type parameters aren't allowed in a catch clause, so I can't think of any way to make that happen. 🙂
@into_the_earth
@into_the_earth 9 ай бұрын
U can do runCatching and catch throwable in the implementation. Then check if the throwable is of type T. If it is of type T, simply return a result, otherwise, rethrow. Arrow-kt uses this in its catch function. They also include a non-fatal check that rethrows any fatal exceptions like out of memory or cancellation. So it’s safe to use with coroutines.
@reosfire
@reosfire 9 ай бұрын
​@@typealiasactually we can write such generic function. It should be inline and should have a try catch catching every exception, but we can return result if 'exception is T' or rethrow exception otherwise
@ArthurKhazbs
@ArthurKhazbs 7 ай бұрын
@@reosfire Haha yes, that's the sort of thing I thought of too when dealing with exceptions
@ArthurKhazbs
@ArthurKhazbs 7 ай бұрын
​@@into_the_earth Would actually be cool to have a Result that would require us to handle (or at least acknowledge) the exact kind of error we may encounter before we get to retrieve a successful value. This way we could have something akin to checked exceptions in Kotlin! :D
@kolyakalyzhny
@kolyakalyzhny 10 ай бұрын
Hi, Dave! Thank you for the nice video. The way I like to conceptualize it is that `runCatching` is just a smaller part of a bigger intent to leverage the power of compiler and the type system. By placing `Result`, `Either`, `Option` etc in the type signature, you address the problem of referential transparency of your functions. Consider other Kotlin features like data classes and sealed hierarchies which allow you to create your own algebraic data types. As far as I am concerned, these and other nice things help you develop a more robust and maintainable system. Besides, other languages like Swift, Rust and some older ones try to incorporate a similar approach. For anyone new to this, consider giving it a try in languages like Scala, Elm, Haskell, F# where this approach works best.
@typealias
@typealias 10 ай бұрын
Hey Kolya, thanks so much! And thanks for sharing your thoughts on this one! I do love the trends of relying more on the type system and compiler for this kind of stuff. Even when not fully embracing functional programming, it's encouraging to see that some of its concepts are becoming more mainstream, especially where type safety and referential transparency are concerned. I've only poked around with Haskell a little bit, but I might have to pick it up again soon for further study!
@thatdougsmith
@thatdougsmith 11 ай бұрын
Thank you for the helpful guidelines on this, Dave! Well done! And I'm so excited to see that your book is available!
@typealias
@typealias 11 ай бұрын
Thank you so much, Doug! 🙂 Lots of fun things happening!
@katarinazzz
@katarinazzz 9 ай бұрын
I LOVE your content, hope you'll continue making videos!
@typealias
@typealias 9 ай бұрын
Thank you so much! Yes, more videos are on the way!
@goobar
@goobar 11 ай бұрын
Nice work! Those results were pretty interesting. I'd be curious if the larger than expected usage of runCatching() was driven be backend developers who are maybe more accustomed to a more functional approach 🤔
@typealias
@typealias 11 ай бұрын
Good thought! I'm guessing backend developers who are deep into FP approaches would use an Either class from Arrow (or elsewhere), but I think there's probably also a category of backend developers who do things in a "sort of FP" way, for whom runCatching() might have a strong appeal.
@Poisonedyouth79
@Poisonedyouth79 11 ай бұрын
Very good overview about the possibilities to deal with exceptions in Kotlin. Personally I prefer using the Either type of Arrow for handling exceptions.
@typealias
@typealias 11 ай бұрын
Thanks so much! Yes, lots of good reasons to go with the Either type!
@filipbeban
@filipbeban 11 ай бұрын
Great video, thanks Dave!
@typealias
@typealias 11 ай бұрын
Thanks so much! Glad you liked it! 🙂
@UsmonWasTaken
@UsmonWasTaken 11 ай бұрын
I actually prefer the way Arrow team handles the exceptions using Either, could be easier to handle nested exceptions.
@typealias
@typealias 11 ай бұрын
Yes - I kind of wish I would have included a third option in the poll for third-party Either implementations, since it seems like many developers - especially those already into FP - prefer those.
@goobar
@goobar 11 ай бұрын
​@@typealiasThat would be an interesting follow-up. I know for me, the runCatching() function was a gateway towards other solutions like Either and comprehensions coming from Arrow
@snowe..
@snowe.. 11 ай бұрын
I had no clue that runCatching with a result type existed in the standard library. I have used other library alternatives though. TIL
@typealias
@typealias 11 ай бұрын
Ah, cool! Yes, I do wonder how much more adoption runCatching() would have if more people knew about it!
@markusmcgee
@markusmcgee 9 ай бұрын
run runCatching etc in Kotlin is the truth
@LakshmanaChilukuri
@LakshmanaChilukuri 11 ай бұрын
In coroutines i need to have without catching anything try { //Code } finally { /*homework*/ }
Does AI Coding Make Programmers Faster
11:27
Ian Sinnott
Рет қаралды 367
Lambda with a receiver in Kotlin
19:36
Caege
Рет қаралды 32
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
5 Fun Ways to Use Extension Functions in Kotlin
18:46
Dave Leeds
Рет қаралды 13 М.
Coroutines: Concurrency in Kotlin
30:22
Dave Leeds
Рет қаралды 19 М.
Kotlin Code Reuse: Composing like you're Inheriting
14:42
Dave Leeds
Рет қаралды 11 М.
Reified Type Parameters
10:15
Dave Leeds
Рет қаралды 4,6 М.
Applying the State Pattern in Kotlin
12:55
Dave Leeds
Рет қаралды 7 М.
Smart Casts with Kotlin Contracts
7:35
Dave Leeds
Рет қаралды 8 М.
Kotlin + Power-Assert = ❤️ | Brian Norman
13:13
Kotlin by JetBrains
Рет қаралды 6 М.
Easy SharedPreferences with Delegated Properties in Kotlin
21:18
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН