0
0
Kotlinprogramming~20 mins

Custom exception classes in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of custom exception throw and catch
What is the output of this Kotlin code that defines and throws a custom exception?
Kotlin
class MyException(message: String) : Exception(message)

fun test() {
    try {
        throw MyException("Oops!")
    } catch (e: MyException) {
        println("Caught: ${e.message}")
    }
}

fun main() {
    test()
}
AException in thread "main" MyException: Oops!
BCaught: null
CCaught: Oops!
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the exception message is passed and printed.
Predict Output
intermediate
2:00remaining
Value of variable after catching custom exception
What is the value of variable result after running this Kotlin code?
Kotlin
class CustomError(msg: String) : Exception(msg)

fun checkValue(x: Int): String {
    return try {
        if (x < 0) throw CustomError("Negative")
        "Positive"
    } catch (e: CustomError) {
        e.message ?: "Error"
    }
}

fun main() {
    val result = checkValue(-5)
    println(result)
}
Anull
BError
CPositive
DNegative
Attempts:
2 left
💡 Hint
Check what message the exception carries and how it's returned.
🔧 Debug
advanced
2:00remaining
Identify the error in custom exception class definition
This Kotlin code tries to define a custom exception but causes a compilation error. What is the error?
Kotlin
class BadException : Exception {
    constructor(message: String) {
        super(message)
    }
}

fun main() {
    throw BadException("Error")
}
AMissing primary constructor call to superclass Exception
Bsuper(message) cannot be called inside constructor body
CException class cannot be inherited
DNo error, code compiles and runs
Attempts:
2 left
💡 Hint
In Kotlin, superclass constructor must be called in constructor header.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a custom exception with default message?
Choose the Kotlin code that correctly defines a custom exception class with a default message "Default error".
Aclass MyError(message: String = "Default error") : Exception(message)
Bclass MyError : Exception { constructor(message: String = "Default error") : super(message) }
Cclass MyError : Exception(message: String = "Default error")
Dclass MyError(message: String) : Exception { init { message = "Default error" } }
Attempts:
2 left
💡 Hint
Look for correct syntax for default parameter and superclass call.
🚀 Application
expert
2:00remaining
How many items in list after filtering custom exceptions?
Given this Kotlin code, how many exceptions in the list are instances of CustomException after filtering?
Kotlin
open class CustomException(msg: String) : Exception(msg)
class AnotherException(msg: String) : Exception(msg)

fun main() {
    val exceptions = listOf(
        CustomException("A"),
        AnotherException("B"),
        CustomException("C"),
        Exception("D")
    )
    val filtered = exceptions.filter { it is CustomException }
    println(filtered.size)
}
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Count only instances of CustomException class.