Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

An Exception is an error that occurs when something unexpected happens during the execution of a program. Programming languages such as Kotlin and Java have some built-in Exception classes that we can use to handle common errors in our code.

In addition, we can also create our own custom Exception classes to handle potential errors specific to our code. Let’s look at how we can do this in Kotlin.

2. Creating a Simple Custom Exception

The easiest way to create a custom exception is to create a class that inherits from the base Exception class:

class MyCustomException(message: String) : Exception(message)

This creates a custom exception class called MyCustomException that we can use like any other Exception class. We can also pass a detailed message while using this exception:

throw MyCustomException("This is a detail message for my custom exception")

The process of catching a custom exception is the same as catching a regular Exception. For example, we can use a try-catch block:

try {
    // throw the exception
    throw MyCustomException("This is a detail message for the custom exception")
} catch (e: MyCustomException) {
    // handle the exception here
}

Please refer to our previous article for more information about Exception handling in Kotlin.

We created a simple custom exception class above. However, our class does not support all the constructors supported by the parent Exception class. Specifically, the Exception class offers four constructors:

class Exception {
...
    constructor ()
    constructor (message: String?)
    constructor (message: String?, cause: Throwable?)
    constructor (cause: Throwable?)
...
}

Similarly, to create a full-featured custom exception, we must override all four constructors in our class like the following:

class MyCustomException : Exception {
    constructor() : super()
    constructor(message: String) : super(message)
    constructor(message: String, cause: Throwable) : super(message, cause)
    constructor(cause: Throwable) : super(cause) 
}

With this modification, our custom exception is now at feature parity with the base Exception class. In other words, we can now use it in any scenario where the default Exception class may be used.

4. Using Default Arguments to Reduce Boilerplate

As we saw, to create a full-featured custom exception, we inherit from the Exception class and override all its constructors. That’s a lot of boilerplate code. Fortunately, Kotlin allows functions to have default arguments, which we can use to reduce the boilerplate:

class CustomException(message: String? = null, cause: Throwable? = null) : Exception(message, cause)

This makes our full-featured custom exception class significantly shorter and much easier to read.

5. Conclusion

As we saw in this article, creating a custom exception is a simple process. We can do this simply by creating a class that inherits from the Exception class and takes a message String as an argument.

In addition, we can also create a full-featured exception class. To do that, we can implement all four constructors that are part of the default Exception class.

As always, the code for these examples can be found over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!