https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

Pablichjenkov

08/06/2019, 7:59 PM
I have the code bellow:
Copy code
suspend fun doSomethingAndThrow() { throw(MyException()) }

val coroutineScope = CoroutineScope(Job())

coroutineScope.launch {
  
  try { doSomethingAndThrow() } catch(exception: Exception) { Log.d("MyException got caught")  }

}
Is the exception thrown in the suspended fun doSomethingAndThrow(…) caught by my try/catch block or it gets caught through the CoroutineScope hierarchy. What would be a good practice to catch this exception?
l

louiscad

08/06/2019, 8:01 PM
This code doesn't compile. After you fix it, you'll receive
MyException
in the
catch
block, as it would do with non suspending code.
👍 1
p

Pablichjenkov

08/06/2019, 8:04 PM
Sorry about that I just type it out of my head. Is more a pseudo code than actual Kotlin code. Thanks for responding.
s

streetsofboston

08/06/2019, 10:06 PM
Maybe this will help you: “Exceptional Exceptions for Coroutines made easy…?” by Anton Spaans https://link.medium.com/rQ01l9CdWY
p

Pablichjenkov

08/06/2019, 10:09 PM
Thanks man, I LL take a look when I got home.
@streetsofboston That medium article is gold man!
👍 1