A try block can have multiple catch blocks. When we are not sure what all exceptions can occur inside the try block then it is always a good idea to have multiple catch blocks for the potential exceptions and in the last catch block have the parent exception class to handle the remaining exceptions that are not specified by catch blocks.
Kotlin multiple catch blocks example
In the following example we have multiple catch blocks but when an exception occurs it looks for the handler for that particular exception.
The exception occurred here is Arithmetic exception, however the first two catch blocks didn’t handle the Arithmetic exception thats why the third catch block’s code executed. Third block is handling all the exceptions because it is using the Exception class which is a parent of all the exception classes.
fun main(args: Array<String>) { try{ var num = 10/0 println(num) } catch(e: NumberFormatException){ println("Number format exception") } catch(e: ArrayIndexOutOfBoundsException){ println("Array index is out of range") } catch(e: Exception){ println("Some Exception occurred") } println("Out of try catch block") }
Output:
Another example of multiple catch blocks
Here is another example of multiple catch blocks, here ArrayIndexOutOfBoundsException occurred, since there is a handler (catch block) present for this exception, the code inside the handler is executed.
fun main(args: Array<String>) { try{ val a = IntArray(5) a[10] = 99 } catch(e: ArithmeticException){ println("ArithmeticException occurred") } catch(e: NumberFormatException){ println("Number format exception") } catch(e: ArrayIndexOutOfBoundsException){ println("Array index is out of range") } catch(e: Exception){ println("Some error occurred") } println("Out of try catch block") }
Output:
Why it is a good idea to have parent Exception class in the last catch block
Lets take the same example that we have above but in this code, we have made a small change. Here we have the handler (catch block) of parent Exception class in the first place.
In the code ArrayIndexOutOfBoundsException occurred and we have the handler for this particular exception but since we have the general Exception class in the first place and it handles all the exceptions so it got executed instead of the catch block that handles ArrayIndexOutOfBoundsException.
The catch blocks are checked in the sequential manner so the first catch block is executed, in fact in case of any exception the first catch will execute which is a bad programming practice because we want a specific message rather than a generalized message. So the solution is to have this default handler at the last place like we did in the above example.
fun main(args: Array<String>) { try{ val a = IntArray(5) a[10] = 99 } catch(e: Exception){ println("Some error occurred") } catch(e: ArithmeticException){ println("ArithmeticException occurred") } catch(e: NumberFormatException){ println("Number format exception") } catch(e: ArrayIndexOutOfBoundsException){ println("Array index is out of range") } println("Out of try catch block") }
Output:
Leave a Reply