Kotlin Try Catch

Despite inheriting the concept of exception from Java, Kotlin also provides checked exceptions. During the runtime execution step of Kotlin, it throws only unchecked expressions. The class “throwable” is the root of all exception classes. The throw term in Kotlin is used to throw the exception object. In the article, we have a concept of try-catch exceptions in Kotlin. We will demonstrate various examples of try-catch blocks and also look at how to utilize try-catch in our code.

What is the Try-Catch Block in Kotlin?

Kotlin Try Catch is utilized to deal with code that may throw an exception during execution. In a try block, enclose any code that could throw an exception, and manage exceptions with catch clauses following the try block. If a run-time exception is not handled properly, the relevant program may be terminated. That’s why it is important to use try to catch exception handling in the Kotlin language.

Syntax of the Try-Catch Block in Kotlin

The general syntax of the Try catch block we used in Kotlin. There are three blocks: try, catch, and finally (optional) block shown below:

try {

Code to be executed

Catch (e: Exception) {

Exception handling


finally {

Optional block


When the statement in the catch block defines what to do, an exception in the try block is thrown. Any statement or function called in try block throws an exception. Commands are moved to the catch block as soon as possible. We can have one or more statements specified in the try block. If the catch block does not have an inner, try statement then we have an outside try statement. After the implementation of the try block and catch block, the finally block is used. If the exception is thrown or caught, the finally block is always implemented. We have finally-block options in Kotlin. The most used try-catch exception in the Kotlin language.

How to Use the Kotlin Try-Catch Block?

To understand the basics of using the try-catch in Kotlin language, we should take a look at the following examples:

Example # 1: Using Try Catch in Kotlin

We’ll try an out-of-bounds array index exception in this example. In the try block, we’ll put the code that throws an array index out of bounds exception, and in the catch block, we’ll put the code that handles the problem.

The main method declaration can be found in the code above. Another function “getInt” has been assigned to an object called “number”. The object “number” is initialized with the string value of float number and passed to the object in the Kotlin println function. Then, in the function “getInt” we have a constructor in which we have created an object as “str” and declare it with String type property. The try block is used in this function with the “returned” keyword. In the try block, we have thrown an exception of using parseInt on “str” which will return a string as its first input. When attempting to convert a string with an incorrect format into a numeric value, the “NumberFormatException” is thrown. When the catch block will catch the exception then “0” will be printed.

The output is “0” as we have provided the impossible conversion.

Example # 2: Utilizing Try Catch as an Expression in Kotlin

The try-catch can be used as an expression; it will be very useful in edge circumstances. The try and throw are expressions in Kotlin. They may be given to a variable, so we can utilize try-catch as an expression. The line of the try or catch block is returned when you use try-catch as an expression.

In the above example, we used the “val” keyword to define a variable. The variable is named “myStr” which contains the string value “kotlin”. We have created another variable as “Result” which has an “Int” property type and “?” is used to check the null value. Then, we have used the try-catch block as an expression that is assigned to the variable “Result”. In the try block, we have deliberately thrown an exception of converting the string to Int type. The “NumberFormatException” is defined in the exception class of the catch block. When this type of exception is caught we will have “-1” printed.

The exception is caught and we have “-1” as an output.

Example # 3: Using Try Catch Finally Block Kotlin

The finally block will run after the try-block and catch-block have finished executing.  But it won’t affect the try-catch block. There is no difference between including the finally block and not including it.

In the above example code, we have used the same implementation of throwing an exception in the try block which is converting the string to the Int value. Then we have finally, a block used which will just execute after the try-catch block. Here in the code, the finally block will not execute because we have an exception caught in the catch block which will cause the program to execute.

Note that the output is not affected if the finally block is written.

Example # 4: Using Several Catch Blocks in Kotlin

Depending on the sort of problems caused by the code in the try block, we can utilize multiple catch blocks in the try block.

We have the main function. The variable “val” is defined as an “array” and it is initialized with the array of six indexed values by using the intArrayOf function. The intArrayOf function provides the integers which are returned in an array. Then, we have a try block in which the exception is thrown as we have passed the index value “8” of the specified array in the println function. After the try block, two catch blocks are used to handle distinct types of exceptions in different ways, such as printing different messages to the user depending on the exception. We have defined exception classes as “ArrayIndexOutOfBoundsException” and the “Exception”. The one catch block is executed in the code. As there is only one exception thrown at a time, and only that exception’s catch block is performed.

The output is shown with the array out of bounds exception as we have six index values array given in the code.


The article was aimed to demonstrate the usage of the Try catch block in Kotlin using Ubuntu 20.04. The try-catch block in Kotlin is helpful for exception handling. This will make our work easier as we can find anything from the large content. We have different example codes of the regex function. These examples will help you a lot in dealing with regex pattern sequences in the Kotlin programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.