Throw Keyword in Kotlin

Learn via video courses
Topics Covered

Overview

The throw Keyword in Kotlin is used to throw an explicit or custom exception. The throw keyword in Kotlin is a primary component of the language’s exception-handling mechanism and helps developers raise certain exceptions during program execution, allowing them to manage unexpected situations and errors in a structured manner.

Introduction

Exception handling is an important aspect of writing reliable and robust code for any software application. Errors and unexpected conditions can arise anytime during the execution of the program, and without proper handling, the exceptions can lead to crashes and undefined behaviour. So, for addressing such challenges Kotlin provides the throw keyword, which allows developers to throw exceptions explicitly when specific error conditions are encountered, allowing for better error management and code robustness.

By using the throw keyword in Kotlin, developers can create and propagate instances of exception classes, which provide information about the type of error and a description of its cause.

Understanding the "throw" Keyword

To understand the throw keyword, We have to understand Exceptions in Kotlin. In Kotlin, there are only unchecked exceptions that are thrown during the runtime execution of the program. All exception classes in Kotlin inherit from the Throwable class and Kotlin uses the throw keyword to throw an exception object.

Note: Kotlin doesn’t support checked exceptions like Java.

We can use the throw keyword in Kotlin to throw a certain exception or a custom exception and also we can use throw as an expression in Kotlin. For example, it can be used as a part of an Elvis expression to handle situations where a value might be null or invalid.

In the above example, the userInput variable is nullable. The Elvis operator ?: is used to return the value of userInput if userInput is not null. If userInput is null, the right-hand side of the Elvis expression is executed, throwing an IllegalArgumentException with the message "Input cannot be null."

The throw expression is of the type Nothing. This type has no values and is used to mark code locations that can never be reached. We can use Nothing to mark a function that never returns.

Syntax and Example

The syntax of the throw keyword in Kotlin to throw an exception object is as follows:

The syntax of the throw statement includes the exception type and an optional error message.

Here is an example to demonstrate the use of the throw keyword in Kotlin:

In this example, we define a custom exception class CustomException that inherits from the built-in Exception class. The function someFunction takes an integer value as an argument. If the value provided is negative, it throws a CustomException with a corresponding message. Otherwise, it prints the value.

When you call to function someFunction with a negative value, the exception is thrown, and the program terminates without reaching the line that prints "After calling someFunction"

Output:

When the input provided is positive:

When the input provided is negative:

Note: It is generally not recommended to use exceptions for control flow like this, Exceptions are meant to handle exceptional situations, and using them for regular flow control can make your code harder to understand and maintain.

Conclusion

  • The throw keyword in Kotlin is used for explicitly raising exceptions during program execution.
  • Exception handling is essential to manage unexpected errors and ensure program robustness.
  • Exceptions are instances of classes that inherit from the Throwable class or its subclasses.
  • The throw statement hinders the normal program flow and transfers control to an appropriate exception handler.
  • The syntax of the throw statement includes the exception type and an optional error message.
  • Developers can create custom exception classes by extending the built-in Throwable class or its subclasses.