0
0
Kotlinprogramming~5 mins

Preconditions (require, check, error) in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the require function do in Kotlin?

require checks a condition and throws an IllegalArgumentException if the condition is false. It is used to check arguments passed to a function.

Click to reveal answer
beginner
How is check different from require in Kotlin?

check verifies a condition and throws an IllegalStateException if false. It is used to check the state of an object, not arguments.

Click to reveal answer
beginner
What exception does error throw in Kotlin?

error throws an IllegalStateException with a given message. It is used to signal an unrecoverable error.

Click to reveal answer
beginner
Example: What happens if require(x > 0) fails?

If x > 0 is false, require throws IllegalArgumentException with a message explaining the failed requirement.

Click to reveal answer
intermediate
When should you use require vs check?

Use require to validate function arguments. Use check to verify the internal state of an object or system.

Click to reveal answer
What exception does require throw if its condition is false?
AIllegalArgumentException
BIllegalStateException
CNullPointerException
DIndexOutOfBoundsException
Which function is best to check if an object's state is valid?
Acheck
Brequire
Cerror
Dassert
What does the error function do in Kotlin?
AIgnores the error
BReturns a boolean
CLogs an error and continues
DThrows an exception with a message
If you want to check a function argument, which precondition function should you use?
Acheck
Berror
Crequire
Dthrow
What exception does check throw when its condition fails?
AIllegalArgumentException
BIllegalStateException
CRuntimeException
DIOException
Explain the difference between require and check in Kotlin.
Think about what each function is validating and the exceptions they throw.
You got /4 concepts.
    Describe a situation where you would use the error function in Kotlin.
    Consider when your program cannot continue safely.
    You got /3 concepts.