Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views7 pages

Kotlin - Exception Handling

The document discusses exception handling in Kotlin using try, catch, throw and finally blocks. It describes the different types of exceptions, how to define try-catch blocks, use the finally block, throw custom exceptions, and implement nested try blocks and multiple catch blocks.

Uploaded by

sranjeeta784
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Kotlin - Exception Handling

The document discusses exception handling in Kotlin using try, catch, throw and finally blocks. It describes the different types of exceptions, how to define try-catch blocks, use the finally block, throw custom exceptions, and implement nested try blocks and multiple catch blocks.

Uploaded by

sranjeeta784
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Kotlin Exception Handling | try, catch, throw and finally

An exception is an unwanted or unexpected event, which occurs during the


execution of a program i.e at run time, that disrupts the normal flow of the
program’s instructions.

There are two types of Exceptions –


1. Checked Exception – Exceptions that are typically set on methods and
checked at the compile time, for example IOException,
FileNotFoundException etc
2. UnChecked Exception – Exceptions that are generally due to logical errors
and checked at the run time, for example NullPointerException,
ArrayIndexOutOfBoundException etc

Kotlin Exceptions –

In Kotlin, we have only unchecked exceptions and can be caught only at run time.
All the exception classes are descendants of Throwable class.
Some of the common exceptions are:
 NullPointerException: It is thrown when we try to invoke a property or
method on null object.
 Arithmetic Exception: It is thrown when invalid arithmetic operations are
performed on numbers. eg – divide by zero.
 SecurityException: It is thrown to indicate security violation.
 ArrayIndexOutOfBoundException: It is thrown when we try to access
invalid index value of an array.

Kotlin try-catch block –

In Kotlin, we use try-catch block for exception handling in the program. The try
block encloses the code which is responsible for throwing an exception and the
catch block is used for handling the exception. This block must be written within
the main or other methods. Try block should be followed by either catch block or
finally block or both.
Syntax for try-catch block –
try {
// code that can throw exception
} catch(e: ExceptionName) {
// catch the exception and handle it
}
EX:

import kotlin.ArithmeticException

fun main(args : Array<String>){


try{
var num = 10 / 0
}
catch(e: ArithmeticException){
// caught and handles it
println("Divide by zero not allowed")
}
}

Kotlin finally block –

In Kotlin, finally block is always executes irrespective of whether an exception is


handled or not by the catch block. So it is used to execute important code
statement.
We can also use finally block with try block and skip the catch block from there.
Syntax:
try {
// code that can throw exception
} catch(e: ExceptionName) {
// catch the exception and handle it.
} finally {
// finally block code
}
EX:
fun main (args: Array<String>){
try {
var int = 10 / 0
println(int)
} catch (e: ArithmeticException) {
println(e)
} finally {
println("This block always executes")
}
}

Kotlin throw keyword –

In Kotlin, we use throw keyword to throw an explicit exception. It can also be


used to throw a custom exception.
Ex:
import java.util.*
class TooOld : Throwable
{
constructor()
{
println("Too old")
}
}
class TooYoung : Throwable
{
constructor()
{
println("Too Young")
}
}
fun main()
{
var i = 15
if(i>80) throw TooOld()
else if (i<18) throw TooYoung()
else println("Eligiable")
}
Kotlin Nested try block and multiple catch block

Nested try block –

Nested try block is a block in which we can implement one try catch block into
another try catch block.
Syntax:
// outer try block
try
{
// inner try block
try
{
// code that can throw exception
}
catch(e: SomeException)
{
// catch the exception and handle it
}
}
catch(e: SomeException)
{
// catch the exception and handle it
}

Multiple catch block –

 A try block can have more than one catch block.


 When we are not sure what type of exception can occur inside the try
block then we can put multiple catch blocks for the potential exceptions
and in the last catch block we can put the parent exception class to handle
all the remaining exceptions, which are not specified by catch blocks in the
program.
 For multiple catch blocks, we can follow either parent-to-child or child-to-
parent exceptions. However, in Java, it should follow a child-to-parent
hierarchy only.
EX:
fun main()
{
try
{
println("Outer try")
var a = 10/0
println("a="+a)
println("Outer try2")
}
catch(e:ArithmeticException)
{
println("Outer catch block")
println(e)
try
{
var a=arrayOf(1,2,3,4,6)
println("value at 6th position="+a[7])
}
catch(e:Exception)
{
println(e)
}
}
catch(e:Exception)
{
println(e)
}
}
O/P:
Outer try
Outer catch block
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 5

You might also like