Try Catch block in Kotlin

In the last tutorial, we have learned about Exception and this tutorial we will learn how we can prevent our code from throwing exceptions. One of the powerful method to handle exceptions in Kotlin is Try-Catch block in Kotlin. The try block is followed by the catch and finally blocks. In this catch and finally blocks, we write code to handle the exception. Please check out the basic syntax of the Try-Catch block.

try{    
//Here is code that may throw an exception    
}catch(ref: Exception_Class){
}    

Before going further, here we write a code(with exception) without Try Catch block:

Example Program(without Try Catch Block)

fun main(args : Array<String>){    
    println("Ans of 10/2 ="+(10/2))
    println("Ans of 12/3 ="+(12/3))
    println("Ans of 5/0 ="+(5/0)) // this will throws ArithmeticException 
    println("Ans of 24/6 ="+(24/6))
    println("Ans of 8/2 ="+(8/2))
}

Output:

Ans of 10/2 =5
Ans of 12/3 =4

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Example.main(Example.java:4)}

As you can see in the above example program we got an ArithmeticException(Because we can’t divide a number by zero in Kotlin) on the fourth line. Now in the next example, we will write the same program but with the Try-Catch block to handle the Arithmetic Exception we got in our last example.

fun main(args : Array<String>){    
    println("Ans of 10/2 ="+(10/2))
    println("Ans of 12/3 ="+(12/3))
    try{
    println("Ans of 5/0 ="+(5/0)) // this will throws ArithmeticException 
    }catch(e: ArithmeticException){
        println(e)
    }
    println("Ans of 24/6 ="+(24/6))
    println("Ans of 8/2 ="+(8/2))
}

Output:

Ans of 10/2 =5
Ans of 12/3 =4
java.lang.ArithmeticException: / by zero
Ans of 24/6 =4
Ans of 8/2 =4

Here you can see how we handled ArithmeticException in our last example using try catch block. In the try block, we placed all the code which might throw an exception and we catch this exception using catch block.

Instead of adding particular exception class such as ArithmeticException or ArrayIndexOutOfBoundsException you can directly add parent Exception class.

Example Program:

fun main(args : Array<String>){    
    println("Ans of 10/2 ="+(10/2))
    println("Ans of 12/3 ="+(12/3))
    try{
    println("Ans of 5/0 ="+(5/0)) // this will throws ArithmeticException 
    }catch(e: Exception){
        println(e)
    }
    println("Ans of 24/6 ="+(24/6))
    println("Ans of 8/2 ="+(8/2))
}

Output:

Ans of 10/2 =5
Ans of 12/3 =4
java.lang.ArithmeticException: / by zero
Ans of 24/6 =4
Ans of 8/2 =4

Multiple catch blocks:

In many situations, your code might throw multiple types of exception so to solve different kind of exception we can add multiple catch block with a try block. Check out the following example.

fun main(args : Array<String>){    
    try{
         val num = arrayOf(5,6,9,8) 
         num[4]=10/0
         println("Done")
     }
     catch(e: ArithmeticException){
       println("Can not divide by Zero")
     }
     catch(e: ArrayIndexOutOfBoundsException){
       println("Out of Array Index")
     }
   println("Outside try-catch block")
   println("Outside try-catch block")
}

Output:

Can not divide by Zero
Outside try-catch block
Outside try-catch block

As in the above example, you can see we have added multiple catch blocks(ArithmeticException and ArrayIndexOutOfBoundsException) and in the above example, we are got handled ArithmeticException. Here is the same example again with little change to show you the concept of multiple catch blocks.

fun main(args : Array<String>){    
    try{
         val num = arrayOf(5,6,9,8) 
         num[6]=10/2
         println("Done")
     }
     catch(e: ArithmeticException){
       println("Can not divide by Zero")
     }
     catch(e: ArrayIndexOutOfBoundsException){
       println("Out of Array Index")
     }
   println("Outside try-catch block")
   println("Outside try-catch block")
}

Output:

Out of Array Index
Outside try-catch block
Outside try-catch block
Spread the love