# Types & Errors (/sdks/kotlin/reference/types-and-errors)



Kotlin messages are built with DSL builders (`eventMessage { }`, `queueMessage { }`, `commandMessage { }`, etc.). Responses map to `EventSendResult`, `QueueReceiveResponse`, `CommandResponse`, and `QueryResponse`.

## Exception hierarchy [#exception-hierarchy]

The Kotlin SDK uses a sealed class hierarchy for type-safe error handling with `when` expressions:

```text
KubeMQException (sealed)
  ├── Connection (retryable)
  ├── Timeout (retryable)
  ├── Authentication (non-retryable)
  ├── Authorization (non-retryable)
  ├── Validation (non-retryable)
  ├── Throttling (retryable)
  ├── Transport (varies)
  ├── Server (varies)
  ├── ClientClosed (non-retryable)
  └── StreamBroken (retryable)
```

## Common properties [#common-properties]

All `KubeMQException` subclasses share:

| Property      | Type            | Description                          |
| ------------- | --------------- | ------------------------------------ |
| `message`     | `String`        | Human-readable error description     |
| `code`        | `ErrorCode`     | Machine-readable error code (enum)   |
| `category`    | `ErrorCategory` | Error category (enum)                |
| `isRetryable` | `Boolean`       | Whether the operation can be retried |

## Type-safe error handling [#type-safe-error-handling]

```kotlin
try {
    client.publishEvent(message)
} catch (e: KubeMQException) {
    when (e) {
        is KubeMQException.Connection -> println("Connection: ${e.message}")
        is KubeMQException.Timeout -> println("Timeout: ${e.message}, duration=${e.duration}")
        is KubeMQException.Authentication -> println("Auth: ${e.message}")
        is KubeMQException.Authorization -> println("Authz: ${e.message}, channel=${e.channel}")
        is KubeMQException.Validation -> println("Validation: ${e.message}, op=${e.operation}")
        is KubeMQException.Throttling -> println("Throttled: ${e.message}")
        is KubeMQException.Transport -> println("Transport: ${e.message}")
        is KubeMQException.Server -> println("Server: ${e.message}, status=${e.statusCode}")
        is KubeMQException.ClientClosed -> println("Closed: ${e.message}")
        is KubeMQException.StreamBroken -> println("Stream: ${e.message}")
    }
}
```

## Connection state [#connection-state]

```kotlin
sealed interface ConnectionState {
    data object Idle : ConnectionState
    data object Connecting : ConnectionState
    data object Ready : ConnectionState
    data class Reconnecting(val attempt: Int) : ConnectionState
    data object Closed : ConnectionState
}
```

## Performance tips [#performance-tips]

1. **Reuse client instances** -- one client per pattern handles all channels
2. **Use batch APIs** for high-throughput queue workloads (`sendQueueMessagesBatch`)
3. **Use `publishEventStream`** for high-throughput event publishing
4. **Use `use { }` blocks** -- all clients implement `Closeable`
5. **Cancel Flow collection** with coroutine cancellation to stop subscriptions
6. **Avoid blocking in `collect`** -- use `launch` for heavy processing

## Resources [#resources]

* [GitHub Repository](https://github.com/kubemq-io/kubemq-kotlin)
* [Maven Central](https://central.sonatype.com/artifact/io.kubemq.sdk/kubemq-sdk-kotlin)
* [Examples](/sdks/kotlin/how-to)

## See Also [#see-also]

* [Kotlin SDK Getting Started](/sdks/kotlin)
