Issue
Say I want to do sth like
val foo = when(bar) {
"one" -> true
"two" -> false
else -> // in Swift it would be `fatalError()`
}
How do I do it?
I'm aware that there are generally better approaches to this kind of problem, but I want to know how to easily induce a crash in Kotlin too :)
Solution
You could throw an exception. This will bubble up the call stack until somebody handles it or you run out of stack frames and the application crashes.
val foo = when(bar) {
"one" -> true
"two" -> false
else -> throw IllegalArgumentException("$bar is Unknown")
}
I just used IllegalArgumentException
here, but you might want to define your own.
Answered By - Todd
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.