Bạn có thể dùng try-catch thông thường bên trong coroutine để xử lý exception cục bộ.
- Với exception không được bắt, dùng
CoroutineExceptionHandlerđể bắt ở cấp coroutine scope.viewModelScopedùng SupervisorJob nên coroutine con thất bại không hủy coroutine anh em, nhưng exception không được bắt sẽ KHÔNG tự động được log — chúng sẽ crash coroutine đó trong im lặng trừ khi bạn thêmCoroutineExceptionHandlerhoặc try-catch. - Structured concurrency đảm bảo exception trong coroutine con được truyền lên đúng cách cho scope cha.
You can use standard try-catch blocks within coroutines to handle exceptions locally.
- For uncaught exceptions, use
CoroutineExceptionHandlerat the coroutine scope level.viewModelScopeuses SupervisorJob so child coroutine failures do not cancel siblings, but uncaught exceptions are NOT automatically logged — they crash the coroutine silently unless you add aCoroutineExceptionHandleror try-catch. - Structured concurrency ensures exceptions in child coroutines are properly propagated to the parent scope.