Kotlin Exception Tutorial With Example

Hello Readers! In this post, we will discuss about what is exception in kotlin ? We will see what is the meaning of exception in programming? How is exception handled in java? How is kotlin exception handling different from exception handling in java? etc.

What is Exception?

Exception is a runtime problem which occurs in the program and causes it to crash abruptly. For example, it will occur when your program contains calculations where a number is being divided by 0. It may occur when your program contains code that tries to access a file in system that does not exist etc.

If such conditions occur, you program will stop executing and terminate abruptly.

Now, imagine that you are using a software that crashes frequently due to such problems. Would you like such softwares? Absolutely NOT!

As a developer, what will you do to avoid such scenarios in your software ? How will you handle such scenarios ?

That’s where exception handling comes into role. You must know how to handle exceptions.

By handling exception does not mean you will guarantee that such conditions won’t occur i.e. Divide by zero condition will never in calculations, there won’t be a condition where request to access a non-existing file is being made etc. You can not stop such conditions to occur.

However, it’s in your hand to stop program from being terminated abruptly when such condition occurs. This is what handling an exception means. By handling exception, you make sure that program won’t terminate abruptly when scenarios arise. Thus, Exception handling allows our program to run even when there is an exception.

So, now we will focus on how to handle exceptions in kotlin. But, before that we would like to know how is exception being handled in java.

Exception in Java

There are two kinds of exceptions in java –
A. Checked Exception
B. UnChecked Exception

A. Checked Exception in Java

Checked exception is detected and thrown by compiler during compilation. For example, IOException, FileNotFoundException, SQLException etc.

How is checked exception handled in java?
There are 2 ways to handle it –
Use try/catch block and enclose the code that are responsible for exception.
Using throws keyword with method signature.

I am not going in detail for the sake of simplicity of the post.

B. UnChecked Exception in Java

Unchecked exception is exception that escapes the notice of compiler. Such exception is generally due to logic errors in the program. For example, ArithmeticException, ArrayIndexOutOfBoundException, NullPointerException, NumberFormatException etc.

Unchecked exceptions are directly or indirectly subclassed from RuntimeException superclass.

Notice that all such exceptions can n’t by caught until program runs. So, unchecked exception is caught at runtime.

How is unchecked exception handled in java?
You must correct your programming logic to handle such exception. There is no other way. You have to debug the code, find error and correct the logic. That’s it.

Kotlin Exception

All kotlin exceptions are unchecked exception. It means they are caught at runtime.

All exception class descend from the Throwable class.
To throw an exception, we use the throw keyword. For example,

throw Exception("Oops… Throwing an exception") 

Some of the common kotlin exceptions are –
NullPointerException: It is thrown when we try to access a property or method of null object.
ArrayIndexOutOfBoundException: It is thrown when we try to access element of invalid index in an array.
Arithmetic Exception: It occurs when invalid arithmetic operations are performed. For example, divide by zero.
SecurityException: It is thrown when there is a security violation.

How to handle Kotlin Exception?

Before learning how to handle exception in kotlin, let’s go through below terms that are used to handle kotlin exception –

A. trytry block contains lines of code that are responsible for exception.
B. catchcatch block is used to catch exception thrown by try block.
C. finallyfinally block always execute whether exception is handled or not.
D. throwthrow keyword is used to throw an exception explicitly.

A general trend to handle kotlin exception is –
Write the code that can are responsible for exception in try block. Handle the exception using appropriate kotlin exception class using catch keyword. Write important code like releasing resources etc. in finally block.

For example,

try {
    var a = (1 / 0) ; 
} catch (e: ArithmeticException) {
    e.printStackTrace()
}

We will see how to handle kotlin exception in more detail in coming post.

Leave a Reply