코루틴 내에서 try-catch혹은 runCathcing 사용시 주의점

WonDDak·2025년 9월 25일

Android

목록 보기
10/10

CancellationException

코루틴의 취소 전파를 위해 이용되는 CancellationExceptionIllegalStateException을 상속받는다.

즉, try-catch 혹은 runCatching 을 통해 작업할 때 CancellationException 에 대한 수동 처리를 하지 않으면,
코루틴의 정상적인 취소 흐름이 왜곡될 수 있다. 결과적으로 취소가 단순 실패처럼 보이거나, 취소 전파가 막혀버리는 문제가 발생할 수 있다.
이를 위해서는 별도 처리를 해줘야 한다.

왜 별도 처리가 필요할까?

CancellationException 은 실패가 아니라 정상적인 취소 신호이다.
하지만 try-catch, runCatching 은 모든 예외를 잡아버리기 때문에, 취소도 일반 실패처럼 취급하기 때문에,이 예외를 만나면 반드시 재던지기(rethrow) 하여 취소 흐름을 유지해야 한다.

안전한 처리 패턴

try-catch

launch {
    try {
        doWork()
    } catch (e: CancellationException) {
        throw e // 취소는 전파
    } catch (e: Exception) {
        ...
    }
}

runCatching

val result = runCatching {
    doWork()
}.onFailure { e ->
    if (e is CancellationException) throw e
}

withContext

val result = withContext(Dispatchers.IO) {
    try {
        fetchRemoteData()
    } catch (e: CancellationException) {
        throw e
    } catch (e: Exception) {
        ...
    }
}

확장함수로 사용

inline fun <R> runCatchingCancellable(block: () -> R): Result<R> = runCatching(block).onFailure { e ->
	if (e is CancellationException) throw e
}


val result = runCatchingCancellable { doWork() }
profile
안녕하세요. 원딱입니다.

0개의 댓글