Exceptions In Kotlin

In this blog post, we will be looking into what are Exceptions, what are its types and how it is handled in the Kotlin language.

What are Exceptions?

An Exception can be explained as an event/condition which a user encounters when the program is executed. This might lead to abruptly termination of the program or even the crash of the application.

What are different types of Exceptions?

Those of you, who have studied Java might classify it into types:

  • Checked Exceptions
  • Unchecked Exceptions

Let’s look into each of them one by one:

Checked Exception

Checked Exceptions are the exceptions that are handled at the compile time. The compiler will give you an error if you do not handle the exception while writing the code. The exceptions can be handled by writing the try/catch block around the lines of code where an exception might be introduced or by using throws keyword around the method definition in java.

Let’s have a look at the simple example of Checked Exception:

If you will try to compile the above code, it will give you compile time error i.e It gives you Checked Exception warning, which you can mitigate by either using the try/catch block or by adding an exception to the method signature.

An important point to note here is that Kotlin does not have support for the Checked Exceptions just like some other programming languages like C++, Objective C, and C# which support only Unchecked Exceptions. Hence, In kotlin, developers do not need to worry about all those try/catch blocks!

Unchecked Exceptions

Unchecked Exceptions are those exceptions which are not checked by the compiler and are introduced in the code due to bad programming style.

All the unchecked Exceptions are subclasses of RuntimeException class. Some of the most common Unchecked Exceptions among many are:

  • ActivityNotFoundException
  • ArrayIndexOutOfBound Exception

Let’s look into one of the simple example of Unchecked Exception where we will be printing the contents of the file

Output of the above program is:

What might have happened if testfile.txt was not present or we had a typo while writing the name? Right, An Application Crash! Since, Kotlin does not support Checked Exception, it becomes responsibility of the programmer to put the following code under try/catch block and log the exception and terminate the application gracefully. Let’s modify our code:

When we execute the above program , we get the following output

Now, you might be wondering what is this @Throws Annotation over the method readFileContents()? Actually, this is a good practice to tell the compiler what exceptions will be thrown by this method as the same will be translated to the JVM method. For example, when the above method will be translated into the JVM method it will look like something like this:

Like in Java, when try/catch block is completed, if followed by finally block that is executed regardless of try/catch result. Generally, this method is used to let go of the memory occupied by variables which might not be needed anymore.

One of the coolest features of the Kotlin language is that Exceptions are also treated as Expressions i.e you can get a value from the Exception Handling method. Let’s look into it with a simple program:

Let’s look at the output, when we supply input as 2

Let’s also look at the output, when we give invalid input

Moving on, What about Custom Exceptions in Kotlin? Can we create Custom Exceptions in the Kotlin language? Let’s look into the same:

If, you look at the above code, the output will be this:

So what exactly happened? So we created a custom class ValidateDataException which extends the RunTimeException and passed the parameter value to the RunTimeException class.

An important point to note here is to send a customized exception we use Throw keyword as shown in the above example and passing your customized message as the parameter of the object of the defined custom class(ValidateDataException).

One more concept which we should have a look is at the Nothing keyword:

Nothing Keyword

The best example to link Nothing is with keyword void of Java. Nothing in kotlin has no instances. It is used to represent a value that never exists. A function which has a return type of Nothing means it returns nothing. Let’s look at the simple example:

So we can say that the method canThrowException return Nothing and is of type void as per java Convention.

I hope with this blog post, you have acquired knowledge of handling Exceptions in Kotlin. Feel free to comment or reach out to me for any queries. Until the next one, stay tuned….

Leave a Reply

Your email address will not be published. Required fields are marked *