August 2017
Intermediate to advanced
440 pages
10h
English
The Kotlin try... catch block is the equivalent of the Java try... catch block. Let's look at a quick example:
fun sendFormData(user: User?, data: Data?) { // 1
user ?: throw NullPointerException("User cannot be null") // 2
data ?: throw NullPointerException("Data cannot be null")
//do something
}
fun onSendDataClicked() {
try { // 3
sendFormData(user, data)
} catch (e: AssertionError) { // 4
// handle error
} finally { // 5
// optional finally block
}
}
Read now
Unlock full access