티스토리 뷰

Android/Kotlin

[Kotlin]  Coroutine  +  CoroutineExceptionHandler

혀가 길지 않은 개발자 2020. 7. 29. 18:27

build.gradle (Module: app)

dependencies {
    // Coroutine
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
}

 

 

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        GlobalScope.launch(Dispatchers.IO) {
            launch {
                throw Exception()
            }
        }
    }

}

앱이 강제로 종료됨.

 

앱이 강제로 종료됨.

 

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val handler = CoroutineExceptionHandler {coroutineContext, throwable ->
            Log.e("", throwable.toString())
        }

        GlobalScope.launch(Dispatchers.IO + handler) {
            launch {
                throw Exception()
            }
        }
    }

}

실행 결과

 

 

 

'Android > Kotlin' 카테고리의 다른 글

[Kotlin]  import  org.json.*  (0) 2020.07.31
[Kotlin]  MVVM  +  Coroutine  (0) 2020.07.30
[Kotlin]  Coroutine  +  ViewModel  (0) 2020.07.29
[Kotlin]  Coroutine (2)  (0) 2020.07.29
[Kotlin]  Thread.join()  (0) 2020.07.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함