Why Kotlin? An Introduction. (Part 3)
Error Handling: Something went wrong...
Lets follow up the discussion about nullability and talk about error handling in Kotlin. Syntactically, the differences from Java are pretty nominal, except for one pretty major key difference that I'll get to in just a second.
First some code to demonstrate how similar Kotlin Exceptions are to Java's Exception handling:
fun doSomething(data : String) { if(data.contains("test")) { throw IllegalStateException("Test!") } } fun main() { try { doSomething("testData") } catch (e : Exception) { // This will always get called because an exception is thrown if a String containing "test" is passed to doSomething(.
[Read More]