Skip to content

Error Handling¤

Base exception classes.

Exceptions¤

All exceptions generated by this library inherit from the Error exception.

Exceptions generated by channels inherit from the ChannelError exception. When there is an attempt to use a closed channel, a ChannelClosedError exception is raised.

Causes¤

When a exception is caused by another exception, for example if the underlying channel was closed while seding or receiving a message, the original exception will be available as the cause of the exception:

try:
    await sender.send(42)
except SenderError as error:
    match error.__cause__:
        case None:
            print("The message couldn't be sent for an known reason")
        case ChannelClosedError() as closed_error:
            print(f"The message couldn't be sent, channel closed: {closed_error}")
        case _ as unknown_error:
            print(f"The message couldn't be sent: {unknown_error}")
Tip

If you are using the async iteration interface for receivers, then you can access the cause of the ReceiverStoppedError exception by explicitly calling receive() on the receiver after the iteration is done:

async for message in receiver:
    print(message)
try:
    await receiver.receive()
except ReceiverStoppedError as error:
    print("The receiver was stopped")
    match error.__cause__:
        case None:
            print("The receiver was stopped without a known reason")
        case ChannelClosedError() as closed_error:
            print(f"The channel was closed with error: {closed_error}")
        case _ as unknown_error:
            print(f"The receiver was stopped due to an unknown error: {unknown_error}")