Kotlin finally Block

Kotlin finally block such block which is always executes whether exception is handled or not. So it is used to execute important code statement.

Kotlin finally Block Example 1

Let's see an example of exception handling in which exception does not occur.

Output:

2
finally block always executes
below codes...

Kotlin finally Block Example 2

Let's see an example of exception handling in which exception occurs but not handled.

Output:

finally block always executes
Exception in thread "main" java.lang.ArithmeticException: / by zero

Kotlin finally Block Example 3

Let's see an example of exception handling in which exception occurs and handled.

Output:

java.lang.ArithmeticException: / by zero
finally block always executes
below codes...

Note: The finally block will not be executed if program exits (either by calling exitProcess(Int) or any error that causes the process to abort).