Why Kotlin? An Introduction. (Part 3)

Error Handling: Something went wrong...

Why Kotlin? An Introduction. (Part 3)

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]

Why Kotlin? An Introduction. (Part 2)

java.lang.NullPointerException at Main.subTitle(Main.java:847)

Why Kotlin? An Introduction. (Part 2)

In the first part of this series I stated I'd very likely start all my new Android projects in Kotlin. Don't get me wrong, I don't hate Java, but why would I make such a bold claim? The answer is null. Or more specifically, the explicit detraction of the use of null that Kotlin promotes. Kotlin has some nice features, but its handling of null variables is what hooked me. [Read More]