0
0
Android Kotlinmobile~10 mins

Error handling patterns in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to catch an exception in Kotlin.

Android Kotlin
try {
    val result = 10 / 0
} catch (e: [1]) {
    println("Error occurred")
}
Drag options to blanks, or click blank then click option'
AString
BBoolean
CInt
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-exception types like String or Int in catch.
2fill in blank
medium

Complete the code to throw an exception when a condition is false.

Android Kotlin
fun checkAge(age: Int) {
    if (age < 18) {
        throw [1]("Age must be 18 or older")
    }
}
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
BNullPointerException
CArrayIndexOutOfBoundsException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated exceptions like IOException for argument checks.
3fill in blank
hard

Fix the error in the code to properly handle a nullable value with try-catch.

Android Kotlin
fun parseNumber(str: String?): Int {
    return try {
        str?.toInt() ?: throw [1]("Null string")
    } catch (e: NumberFormatException) {
        0
    }
}
Drag options to blanks, or click blank then click option'
AIOException
BIllegalArgumentException
CNullPointerException
DArithmeticException
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing unrelated exceptions like IOException for null checks.
4fill in blank
hard

Fill both blanks to create a try-catch block that logs the error message.

Android Kotlin
try {
    val data = fetchData()
} catch (e: [1]) {
    Log.e("Error", e.[2])
}
Drag options to blanks, or click blank then click option'
AException
Bmessage
Ccause
DstackTrace
Attempts:
3 left
💡 Hint
Common Mistakes
Using cause or stackTrace instead of message for error text.
5fill in blank
hard

Fill all three blanks to define a custom exception class and throw it.

Android Kotlin
class [1](message: String) : [2](message)

fun test() {
    throw [3]("Custom error")
}
Drag options to blanks, or click blank then click option'
AMyException
BException
DThrowable
Attempts:
3 left
💡 Hint
Common Mistakes
Using Throwable directly or mismatching class names.