0Pricing
Kotlin Academy · Lesson

Creating Custom Exception Classes

Define domain-specific exceptions with meaningful messages and properties.

Why Custom Exceptions?

Custom exceptions encode domain meaning: callers can catch them specifically, get useful messages, and act differently based on type.

Simplest Custom Exception

Subclass Exception (or a specific subclass like RuntimeException) and pass a message to the super constructor.

class InvalidEmailException(message: String) : Exception(message)
fun main() {
    try {
        throw InvalidEmailException("missing @")
    } catch (e: InvalidEmailException) {
        println("caught: ${e.message}")
    }
}

All lessons in this course

  1. try/catch/finally as an Expression
  2. Creating Custom Exception Classes
  3. runCatching and Result
  4. Re-throwing and Exception Chaining
← Back to Kotlin Academy