Javatpoint Logo
Javatpoint Logo

Kotlin Multiple catch Block

We can use multiple catch block in our code. Kotlin multiple catch blocks are used when we are using different types of operation in try block which may causes different exceptions in try block.

Kotlin multiple catch block example 1

Let's see an example of multiple catch blocks. In this example we will are performing different types of operation. These different types of operation may generate different types of exceptions.

Output:

arithmetic exception catch
code after try catch...

Note: At a time only one exception is occured and at a time only one catch block is executed.

Rule: All catch blocks must be placed from most specific to general i.e. catch for ArithmeticException must come before catch for Exception.

What happen when we catch from general exception to specific exception?

It will generate warning. For example:

Let's modify above code and place catch block from general exception to specific exception.

Output at compile time

warning : division by zero
a[5] = 10/0

Output at run time

parent exception catch
code after try catch...





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA