Using Preconditions in Kotlin with require, check, and error
📖 Scenario: You are building a simple Kotlin program that processes user input for an online store. You want to make sure the input values are valid before continuing.
🎯 Goal: Learn how to use Kotlin's require, check, and error functions to validate input and handle errors clearly.
📋 What You'll Learn
Create variables with exact names and values as instructed
Use require to check input preconditions
Use check to verify internal state
Use error to throw an exception with a message
Print the final valid message
💡 Why This Matters
🌍 Real World
Preconditions help ensure that your program only runs with valid data, preventing bugs and crashes early.
💼 Career
Many jobs require writing safe and reliable code that checks inputs and internal states using preconditions and error handling.
Progress0 / 4 steps
1
Set up user input variables
Create a variable called username and set it to the string "Alice". Create another variable called age and set it to the integer 25.
Kotlin
Hint
Use val to create variables with the exact names and values.
2
Add precondition checks with require
Use require to check that username is not empty with the message "Username cannot be empty". Use require to check that age is greater than or equal to 18 with the message "Age must be at least 18".
Kotlin
Hint
Use require(condition) { "message" } to check input values.
3
Check internal state with check and throw error
Use check to verify that username starts with an uppercase letter with the message "Username must start with uppercase". If age is greater than 100, use error to throw an exception with the message "Age is unrealistically high".
Kotlin
Hint
Use check(condition) { "message" } for internal checks and error("message") to throw exceptions.
4
Print confirmation message
Print the message "User Alice is valid and age 25 is accepted." using println.
Kotlin
Hint
Use println("message") to show the final confirmation.
Practice
(1/5)
1. What is the primary purpose of the require function in Kotlin?
easy
A. To log warnings without stopping the program
B. To verify the internal state of the program
C. To stop the program immediately with an error message
D. To check if function arguments meet certain conditions
Solution
Step 1: Understand require usage
require is used to check if the inputs to a function are valid before proceeding.
Step 2: Differentiate from other preconditions
check is for program state, and error stops the program immediately. require is specifically for argument validation.
Final Answer:
To check if function arguments meet certain conditions -> Option D
Quick Check:
require = argument check [OK]
Hint: Require checks function inputs are valid before running [OK]
Common Mistakes:
Confusing require with check for program state
Thinking error is for argument validation
Assuming require stops program immediately
2. Which of the following is the correct syntax to use check in Kotlin to verify a condition with a lazy message?
easy
A. check(value > 0, "Value must be positive")
B. check { value > 0, "Value must be positive" }
C. check(value > 0) { "Value must be positive" }
D. check(value > 0); "Value must be positive"
Solution
Step 1: Recall check function signature
check takes a Boolean condition and a lambda returning a message string if false.
Step 2: Match syntax with options
check(value > 0) { "Value must be positive" } uses check(condition) { "message" } which is correct Kotlin syntax.
Final Answer:
check(value > 0) { "Value must be positive" } -> Option C
Quick Check:
check syntax = condition + lambda message [OK]
Hint: Use check(condition) { "message" } syntax [OK]
Common Mistakes:
Passing message as second argument instead of lambda
Using comma inside lambda incorrectly
Omitting braces for message lambda
3. What will be the output of this Kotlin code?
fun test(x: Int) {
require(x > 0) { "x must be positive" }
check(x < 10) { "x must be less than 10" }
println("x is valid: $x")
}
test(15)
medium
A. IllegalStateException: x must be less than 10
B. IllegalArgumentException: x must be positive
C. No output, program stops silently
D. x is valid: 15
Solution
Step 1: Evaluate require(x > 0)
Since 15 > 0, require passes without exception.
Step 2: Evaluate check(x < 10)
15 is not less than 10, so check throws IllegalStateException with message "x must be less than 10".
Final Answer:
IllegalStateException: x must be less than 10 -> Option A
Quick Check:
require passes, check fails with IllegalStateException [OK]