https://kotlinlang.org logo
#announcements
Title
# announcements
l

LastExceed

09/04/2019, 10:59 AM
Copy code
runBlocking {
        try {
            val listener = aSocket(ActorSelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)).tcp().bind(InetSocketAddress(12345))
            val socket = listener.accept()
            println("client connected")
            socket.openReadChannel().readInt()
        }
        catch (ex: Throwable) {
            println("client disconnected")
        }
    }
when i connect to this and then disconnect it throws a `CancellationException`which is caused by an
IOException
. More specifically, it is thrown in
readInt()
because it's trying to read data from a closed stream. I know i can and should only catch that specific exception at that specific line, but for the example code i wrapped the entire thing in catch Throwable to make double sure it catches everything. But even though I catch it and the catch block is executed just fine the exception is still thrown into the console. why is that?
s

streetsofboston

09/04/2019, 11:48 AM
Maybe something inside the implementations of
listener
and/or
socket
are logging the exception before throwing it?
l

LastExceed

09/04/2019, 11:49 AM
no, it is logged after its thrown
"client disconnected" appears first
sockets are from ktor btw
s

streetsofboston

09/04/2019, 1:52 PM
What happens if you put a
try
around the
runBlocking
and
catch
any Throwables it may throw?