so we should be catching `JobCancellationException...
# coroutines
b
so we should be catching
JobCancellationException
instead of
CancellationException
?
e
It does not matter much if you don’t integrate with Java
CompletableFuture
-based async code. We only throw
JobCancellationException
, but support any
CancellationException
for interop with Java.
b
ah, ok
e
Btw, what is your use-case for catching it manually?
b
i've logged it in the past, but for my current application I change status: ```
Copy code
} catch (e: JobCancellationException) {
            statusChannel.offer(ConnectionStatus.Disconnected)
            throw e
        }
e
What do you send to statusChannel if it crashes with (other) exception?
b
Copy code
catch (t: Throwable) {
            statusChannel.offer(ConnectionStatus.Error("exception thrown: ${t.message}"))
            throw t
        }
my general pattern for doing communication so far has been to have a
run
function that connects and then suspends while the connection is active. It can then be disconnected by canceling from the caller.
e
I’d refactor it as:
Copy code
catch (t: Throwable) {     statusChannel.offer(t.toStatusMessage())
  throw t
}
b
not sure if there's a better way, but this way I don't have to keep track of jobs inside my class
you'd just wrap a
when(t)
in an extension method instead of multiple
catch
blocks? if it's only used in one location whats that buy you? readability?
e
Yes. Readability. This way I immediately see that a status message is sent to the status channel on any exception.