Kotlin Exception Handling
In this tutorial we will learn about Exception Handling in Kotlin. Almost all programming languages provides a mechanism to handle the exceptions. Before starting Exception Handling, let us first discuss what is an Exception.
What is an Exception?
Exception is an event(or an error) which disrupts the normal flow of the program. Exceptions occur at runtime and terminates the program abruptly. Exceptions can occur due to many reasons like:
What is Exception Handling?
Exception Handling is the technique to handle the exceptions which may occur at runtime. If we want to make our program robust, then the exceptions must be handled. Exception handling saves our program from terminating abruptly and enables us to provide a meaningful response to the end user, explaining why the exception has occurred, rather than showing them the exception message (along with strack trace) itself.
All exception classes in Kotlin are descendants of the class Throwable.
In Java, there were two types of exceptions:
Checked Exceptions: Checked Exception are checked at compile time. These exceptions are compulsory to handle either using the try/catch block or throwing the exception further by mentioning
throws keyword in the method definition. The program will not work if we haven't handled the checked exception.
Unchecked Exceptions: These exceptions are not checked at compile time. Even if we don't handle these exceptions, the program will compile successfully.
In Kotlin, we have only unchecked exceptions i.e. checked exceptions are not present in Kotlin. One of the reasons behind eliminating checked exceptions is that large part of developer community thinks that checked exception increases unnecessary code.
If we want to make the caller aware about the exceptions a function might throw, we can add
@Throws annotation in Kotlin.
Exception Handling in Kotlin
Exception handling in Kotlin can be understood using four keywords. They are:
Try: The block of code due to which an exception can occur is placed inside the try block. The try block must be followed by at least one catch block or one finally block or both.
Catch: It is placed after the try block. It catches the exception thrown by the code in the try block. It is the block where the exception is actually handled. We can have multiple catch block after a try block.
Finally: Finally block is always executed even if the exception is not caught or exception does not occur at all. Finally block is used to carry out some important operations like closing an opened file. It is placed after the try or catch block. There can only be one finally block.
Throw: Throw keyword is used to throw an exception explicitly. If developer wants to throw an exception in some situation, we can use throw keyword.
In the upcoming tutorials we will cover all them in details along with code examples.
In this tutorial we learned about, what an exception is and the difference between types of exception in Java and Kotlin. From the next tutorial we will cover how the handle exceptions in Kotling using
throw keywords in detail.