An exception is an undesired or unexpected event that arises during programme execution, or run time, that interferes with the regular flow of the instructions in the programme . Exception handling is a method by which we may manage mistakes and avert programme crashes that could occur during runtime.
There are two type of exceptions-
1. Checked Exception - This exception come from while code in run time like IOException.
2. UnChecked Exception - This exception come from run time like NullPointerException,etc
Common exception mentioned below
- NullPointerException: when object is null then execute this Exception.
- ArithmeticException: when any digit divide by zero then execute this exception
- SecurityException: it is thrown to indicate security violation.
- ArrayIndexOutofBoundException: it is throw when array of bound exception.
Exception of arrayIndexoutofboundException :
Output
Kotlin try-catch block-
In Kotlin, we used a try-catch block for exception handling in the program, try block writing the code, and then throwing exceptions while in the catch block.
Syntax
- ArithmeticException
Output
- Finally block
Happy coding!