본문 바로가기
[Coroutine] Exception handling in coroutine # coroutine builder 에 exception 이 발생하면 parent 를 cancel 하고, parent 가 cancel 되면 모든 children 을 cancel 시킨다. (exception propagation) Stop breaking my coroutines # Exception propagation 은 job 을 기반으로 작동하기 때문에 try-catch 로 잡히지 않는다. SupervisorJob # SupervisorJob 은 한 child 의 exception 이 parent 와 다른 children 을 cancel 시키지 않는다. # 일반적인 실수는 parent coroutine 에 SupervisorJob 을 전달하는 것이다. 예를 들면 launch 에 SupervisorJ.. 2022. 1. 30.
[coroutine] exception 을 이해해보자 #2 ( in global scope ) -[coroutine] exception 을 이해해보자 #1 ( in my coroutine scope ) 과 동일한 sample 들을 직접 생성한 coroutine 이 아닌 GlobalScope 에서 수행해보기로 했다.혹시나 뭔가 동작이 다를까 해서..내가 알고 있기로는 GlobalScope 은 defaultHandler 가 setting 되어 결국 모든 exception 을 먹어 crash 가 나지 않는다는 차이만 있는것으로 알고 있는데.. 검증해보자 ( 결론은 잘못 알고 있었다. ) -class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState).. 2019. 3. 6.
[coroutine] exception 을 이해해보자 #1 ( in my coroutine scope ) -이 글은 coroutine exception 에 대한 일종의 심화과정이라고 볼 수 있다.기본 exception 에 대한 내용을 먼저 이해한 후 이곳의 글을 보자![coroutine] Exception handling -class MainActivity : Activity() { private val coroutineScope = CoroutineScope(Dispatchers.Main) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) findViewById(R.id.btn).setOnClickListener { try { co.. 2019. 3. 5.
[coroutine] Exception handling Exception propagation - Coroutine builder 는 2가지 flavor 가 있다. exception 을 자동으로 전파하는 launch, actor류. 그리고 user 에게 노출되는 async, produce 류. 자동 전파하는 케이스는 Java 의 uncaughtExceptionHandler 와 비슷하게 unhandled exception 이다. 반면에 user 에게 노출되는 경우는 user 가 exception 을 직접 handle 할 수 있다. (돼왕: try-catch 할 수 있다.) 대표적 api 는 await 와 receive 가 있다. - runBlocking{ val job = GlobalScope.launch{ println(“Throwing exception f.. 2019. 3. 4.
반응형