Kotlin Exception Handling

29. Exception Handling 29.1 Kotlin Try Catch 29.2 Kotlin Multiple Catch Block 29.3 Kotlin Throw Exception 29.4 Kotlin Finally Block


Kotlin Exception Handling

Exception provides a unified mechanism for handling errors in an extensible, maintainable and object oriented way. Kotlin exception handling is identical to java exception except in kotlin all exception are unchecked. Unchecked exception are those that does not need to be part of the method signature.


Kotlin Try Catch

In exception handling, code is executed inside the try catch code. The code is enclosed within a try block, to facilitate the catching of prospective exceptions. If something goes wrong inside the try block, you can then throw the exception and your code will then catch the exception and respond accordingly. So all the code you need to handle safety can be enclosed within try block. We can add zero or more catch blocks to handle different exceptions. Finally block is optional which is always executed whether an exception generated or not. In kotlin, all exception classes are descendants of the class Throwable.

Syntax

try{
	someCode()
}catch(e: Exception) {
	handle(e)
}

Example

fun main(args : Array<String>)  { 
	numberValidator(50)
}
fun numberValidator(num: Int){
    val result = try {
        if(num < 0) {
            throw IllegalArgumentException()
        }
        true
    } catch(e: IllegalArgumentException){
        false
    }
    println(result)
}
Output
true




Kotlin Multiple Catch Block

We can also place multiple catch blocks to handle exceptions.

Syntax

try{
	someCode()
}catch(e1: Exception) {
	handle(e1)
}catch(e2: Exception) {
	handle(e2)
}
Example
fun main(args : Array<String>)  { 
	numberValidator(50)
}
fun numberValidator(num: Int){
    val result = try {
        if(num < 0) {
            throw Exception("throw an exception.");
        }
        true
    } catch(e: Exception)
    {
        println("Error Message - ${e.message}")
    }
    catch(e1: Exception)
    {
        println("Error Message - ${e1.message}")
    }
    println(result)
}
Output
true

Kotlin Throw Exception

The throw expression is used to throw an exception object.

Example

fun main(args : Array<String>)  { 
	numberValidator(50)
}
fun numberValidator(num: Int){
    val result = try {
        throw Exception("throw an exception.")
    }
    catch(e: Exception)
    {
        println("Error Message - ${e.message}")
    }
    finally {
        println("This block is always executable.")
    }
    println(result)
}
Output
Error Message - throw an exception.
This block is always executable.
kotlin.Unit


Kotlin Finally Block

Finally block is optional in a try catch block.

Syntax

try {

}
catch (e: Exception) {

}
finally {
	// optional finally block
}

Example

fun main(args : Array<String>)  { 
	numberValidator(50)
}
fun numberValidator(num: Int){
    val result = try {
        if(num < 0) {
            throw Exception("throw an exception.");
        }
        true
    } catch(e: Exception)
    {
        println("Error Message - ${e.message}")
    }
    finally {
        println("This block is always executable.")
    }
    println(result)
}

Output
This block is always executable.
true







Read more articles


General Knowledge



Learn Popular Language