0
0
Kotlinprogramming~20 mins

Why error handling matters in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code with try-catch?

Look at this Kotlin code. What will it print?

Kotlin
fun main() {
    val numbers = listOf(1, 2, 0, 4)
    for (num in numbers) {
        try {
            val result = 10 / num
            println("Result: $result")
        } catch (e: ArithmeticException) {
            println("Cannot divide by zero")
        }
    }
}
A
Result: 10
Result: 5
Result: 0
Result: 2
B
Result: 10
Result: 5
Cannot divide by zero
Result: 2
C
Result: 10
Result: 5
Exception in thread "main" java.lang.ArithmeticException: / by zero
D
Cannot divide by zero
Result: 10
Result: 5
Result: 2
Attempts:
2 left
💡 Hint

Think about what happens when dividing by zero inside the try block.

🧠 Conceptual
intermediate
1:30remaining
Why should programs handle errors?

Why is it important for programs to handle errors properly?

ATo prevent the program from crashing and provide meaningful feedback to users
BTo make the program run faster by skipping error checks
CTo hide all errors so users never know something went wrong
DTo make the code shorter and simpler
Attempts:
2 left
💡 Hint

Think about user experience and program stability.

🔧 Debug
advanced
2:30remaining
Identify the error in this Kotlin error handling code

Find the mistake in this Kotlin code that tries to handle exceptions.

Kotlin
fun main() {
    try {
        val text = "abc"
        val number = text.toInt()
        println(number)
    } catch (e: NumberFormatException) {
        println("Conversion failed")
    } catch (e: Exception) {
        println("General error")
    } catch (e: Throwable) {
        println("Unknown error")
    }
}
AThe code will cause a compile error because toInt() is unsafe
BThe code is correct and will print "Conversion failed"
CThe catch blocks are in the wrong order; Throwable should be last
DThe catch blocks should be combined into one catch block
Attempts:
2 left
💡 Hint

Think about the order of catch blocks from specific to general exceptions.

📝 Syntax
advanced
1:30remaining
What error does this Kotlin code produce?

What error will this Kotlin code cause?

Kotlin
fun main() {
    try {
        val list = listOf(1, 2, 3)
        println(list[5])
    } catch (e: IndexOutOfBoundsException) {
        println("Index error caught")
    } catch (e: Exception) {
        println("Other error")
    }
}
AIndex error caught
BOther error
CNo output, program crashes
DSyntax error: missing colon in catch
Attempts:
2 left
💡 Hint

What happens when you access an invalid list index?

🚀 Application
expert
3:00remaining
What is the value of 'result' after this Kotlin function runs?

Consider this Kotlin function that handles errors. What will be the value of result after calling safeDivide(10, 0)?

Kotlin
fun safeDivide(a: Int, b: Int): Int {
    return try {
        a / b
    } catch (e: ArithmeticException) {
        -1
    } finally {
        println("Operation attempted")
    }
}

fun main() {
    val result = safeDivide(10, 0)
    println("Result is $result")
}
A10
B0
CException thrown, no value returned
D-1
Attempts:
2 left
💡 Hint

Remember what the catch block returns and how finally works.