Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Kotlin Throw Keyword
Kotlin throw keyword is used to throw an explicit exception. It is used to throw a custom exception.
To throw an exception object we will use the throw-expression.
Syntax of throw keyword
throw SomeException() throw SomeException()
Kotlin throw example
Let's see an example of throw keyword in which we are validating age limit for driving license.
fun main(args: Array< String>) { validate(15) println("code after validation check...") } fun validate(age: Int) { if (age < 18) throw ArithmeticException("under age") else println("eligible for drive") } fun main(args: Array< String>) { validate(15) println("code after validation check...") } fun validate(age: Int) { if (age < 18) throw ArithmeticException("under age") else println("eligible for drive") }
Output:
Exception in thread "main" java.lang.ArithmeticException: under age