Bird
Raised Fist0
Kotlinprogramming~20 mins

RunCatching for safe execution in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RunCatching Master
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 RunCatching example?
Consider this Kotlin code using runCatching. What will it print?
Kotlin
val result = runCatching {
    val number = "123a".toInt()
    number * 2
}.getOrElse {
    -1
}
println(result)
AException in thread "main" java.lang.NumberFormatException
B0
C-1
D246
Attempts:
2 left
💡 Hint
Think about what happens when toInt() fails inside runCatching.
❓ Predict Output
intermediate
2:00remaining
What does this runCatching block return?
Analyze this Kotlin snippet using runCatching. What is the value of output?
Kotlin
val output = runCatching {
    val list = listOf(1, 2, 3)
    list[5]
}.getOrNull()
println(output)
AIndexOutOfBoundsException
Bnull
C3
D5
Attempts:
2 left
💡 Hint
What happens when you access an invalid index inside runCatching?
🔧 Debug
advanced
2:00remaining
Why does this runCatching block not catch the exception?
Look at this Kotlin code. Why does the exception still crash the program?
Kotlin
val result = runCatching {
    throw IllegalStateException("Error happened")
}.getOrElse {
    throw it
}
println("Done")
ABecause getOrElse rethrows the exception, so it is not handled.
BBecause println("Done") is unreachable and causes a compile error.
CBecause the exception is thrown outside runCatching block.
DBecause runCatching only catches IOException, not IllegalStateException.
Attempts:
2 left
💡 Hint
Check what getOrElse does with the caught exception.
📝 Syntax
advanced
2:00remaining
Which option correctly uses runCatching to safely parse an integer?
Choose the Kotlin code snippet that safely parses a string to an integer using runCatching and returns 0 if parsing fails.
Aval number = runCatching { "42".toInt() }.getOrElse { 0 }
Bval number = runCatching { "42".toInt() } ?: 0
Cval number = runCatching { "42".toInt() }.getOrDefault(0)
Dval number = runCatching { "42".toInt() }.getOrNull() ?: 0
Attempts:
2 left
💡 Hint
Remember how getOrElse works compared to getOrDefault and getOrNull.
🚀 Application
expert
3:00remaining
How many items are in the list after this runCatching chain?
Given this Kotlin code, how many items does results contain after execution?
Kotlin
val inputs = listOf("10", "abc", "20", "-5")
val results = inputs.mapNotNull { input ->
    runCatching { input.toInt() }
        .getOrNull()
}.filter { it > 0 }
A3
B1
C4
D2
Attempts:
2 left
💡 Hint
Count how many strings convert to positive integers successfully.

Practice

(1/5)
1. What is the main purpose of runCatching in Kotlin?
easy
A. To execute code and catch exceptions safely without crashing the program
B. To run code only if a condition is true
C. To create a new thread for running code
D. To convert a value to a string safely

Solution

  1. Step 1: Understand what runCatching does

    runCatching runs a block of code and catches any exceptions that happen inside it.
  2. Step 2: Identify the main purpose

    This helps keep the program running smoothly by handling errors safely without crashing.
  3. Final Answer:

    To execute code and catch exceptions safely without crashing the program -> Option A
  4. Quick Check:

    runCatching = safe execution [OK]
Hint: runCatching catches errors to keep code safe [OK]
Common Mistakes:
  • Thinking runCatching runs code conditionally
  • Confusing runCatching with threading
  • Assuming it converts types automatically
2. Which of the following is the correct syntax to use runCatching in Kotlin?
easy
A. val result = runCatching -> someFunction()
B. val result = runCatching(someFunction())
C. val result = runCatching { someFunction() }
D. val result = runCatching.someFunction()

Solution

  1. Step 1: Recall the syntax of runCatching

    runCatching takes a lambda block with curly braces to run code safely.
  2. Step 2: Match the correct syntax

    val result = runCatching { someFunction() } uses curly braces and calls someFunction() inside, which is correct.
  3. Final Answer:

    val result = runCatching { someFunction() } -> Option C
  4. Quick Check:

    runCatching uses braces { } [OK]
Hint: Use curly braces {} to pass code block to runCatching [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Trying to call runCatching like a function with arrow
  • Accessing runCatching as an object
3. What will be the output of this Kotlin code?
val result = runCatching { 10 / 0 }
println(result.isSuccess)
medium
A. true
B. null
C. Exception thrown
D. false

Solution

  1. Step 1: Analyze the code inside runCatching

    The code tries to divide 10 by 0, which causes an exception (ArithmeticException).
  2. Step 2: Understand runCatching result

    runCatching catches the exception, so no crash happens. The isSuccess property will be false because the block failed.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    Exception caught means isSuccess = false [OK]
Hint: Divide by zero causes failure, so isSuccess is false [OK]
Common Mistakes:
  • Expecting true because code runs
  • Thinking exception is thrown outside runCatching
  • Confusing isSuccess with isFailure
4. Identify the error in this Kotlin code using runCatching:
val result = runCatching {
    val number = "abc".toInt()
}
println(result.getOrNull())
medium
A. No error, prints null because conversion fails
B. Throws NumberFormatException outside runCatching
C. Syntax error in runCatching block
D. getOrNull() returns the string "abc"

Solution

  1. Step 1: Understand the code inside runCatching

    Trying to convert "abc" to Int causes NumberFormatException, but runCatching catches it.
  2. Step 2: Check what getOrNull() returns

    Since exception happened, getOrNull() returns null safely without crashing.
  3. Final Answer:

    No error, prints null because conversion fails -> Option A
  4. Quick Check:

    Exception caught means getOrNull() = null [OK]
Hint: runCatching catches exceptions, getOrNull returns null on failure [OK]
Common Mistakes:
  • Expecting exception to crash program
  • Thinking getOrNull returns original string
  • Assuming syntax error in code block
5. You want to safely parse a list of strings to integers using runCatching and collect only the successful results. Which Kotlin code snippet correctly does this?
hard
A. val numbers = list.map { runCatching { it.toInt() }.exceptionOrNull() }
B. val numbers = list.map { runCatching { it.toInt() }.getOrNull() }.filterNotNull()
C. val numbers = list.map { it.toInt() }.filter { it != null }
D. val numbers = list.map { runCatching { it.toInt() }.getOrThrow() }

Solution

  1. Step 1: Understand the goal

    We want to convert strings to integers safely, ignoring failures, and collect only successful integers.
  2. Step 2: Analyze each option

    val numbers = list.map { runCatching { it.toInt() }.getOrNull() }.filterNotNull() uses runCatching to catch exceptions, then getOrNull() returns integer or null, and filterNotNull() removes nulls, collecting only successful parses.
  3. Final Answer:

    val numbers = list.map { runCatching { it.toInt() }.getOrNull() }.filterNotNull() -> Option B
  4. Quick Check:

    Safe parse + filter null = val numbers = list.map { runCatching { it.toInt() }.getOrNull() }.filterNotNull() [OK]
Hint: Use getOrNull() then filterNotNull() to keep successful parses [OK]
Common Mistakes:
  • Using getOrThrow() which throws on failure
  • Filtering nulls before catching exceptions
  • Using exceptionOrNull() which returns exceptions, not values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard