Kotlin program of using throw keyword
fun main(args: Array<String>) {
ThrowDemo("Hello, world!")
ThrowDemo("Hello!")
}
// ThrowDemo() function to check
// whether string's lenght is
// equal to or greater than 8
fun ThrowDemo(str: String) {
// Comparing the lenght of the string
if (str.length < 8)
throw ArithmeticException("The string is too short.")
else
println("The string lenght is >=8.")
}
Output:
The string lenght is >=8.
Exception in thread "main" java.lang.ArithmeticException: The string is too short.
at FileKt.ThrowDemo (File.kt:13)
at FileKt.main (File.kt:4)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2)
