Exceptions are events that occur during the execution of a program that disrupt the normal flow of instructions. When an exception is thrown, it creates an exception object that contains information about the error, such as the type of exception and the state of the program when the error occurred.
Exceptions can be caused by many things, including but not limited to:
There are two types of exceptions in Kotlin: unchecked and checked. Unchecked exceptions are those that extend the RuntimeException
class, and checked exceptions are those that extend the Exception
class.
Exceptions can be thrown manually using the throw
keyword. This is useful when you want to abort the execution of a program due to an error. For example, let's say we have a function that calculates the average of two numbers. If the user passes in an invalid input, we want to throw an exception.
fun average(a: Int, b: Int): Double {
if (a < 0 || b < 0) {
throw IllegalArgumentException("Numbers must be positive.")
}
return (a + b) / 2.0
}
In the code above, we throw an IllegalArgumentException
if the inputs are negative. Now, if we try to call the average
function with negative numbers, an exception will be thrown and the program will be aborted.
average(-1, 2) // Throws an IllegalArgumentException
Exceptions can be caught using the try
/catch
keywords. This is useful for gracefully handling errors and continuing the execution of the program. For example, let's say we want to catch the exception thrown by our average
function and print an error message to the user.
fun average(a: Int, b: Int): Double {
if (a < 0 || b < 0) {
throw IllegalArgumentException("Numbers must be positive.")
}
return (a + b) / 2.0
}
fun main(args: Array<String>) {
val a = -1
val b = 2
try {
val avg = average(a, b)
println("The average of $a and $b is $avg")
} catch (e: IllegalArgumentException) {
println("Error: ${e.message}")
}
}
In the code above, we catch the IllegalArgumentException
thrown by our average
function. We then print an error message to the user.
try
/catch
keywords.throw
keyword.RuntimeException
class.Exception
class.