throw Exception in Kotlin
On this page (12sections)
Introduction
throw Exception is a fundamental concept every Kotlin developer should understand. Exceptions signal that something went wrong at runtime. Kotlin treats checked exceptions differently from Java, encouraging explicit error handling where it matters.
Throw creates and raises an exception at runtime. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- throw creates and raises an exception at runtime.
- It stops normal flow and transfers control to the nearest catch block.
- Use throw for invalid input or unexpected states.
Syntax
throw IllegalArgumentException("message")
throw Exception in Kotlin Example Program in Kotlin
fun validateAge(age: Int) {
if (age < 0) throw IllegalArgumentException("Age cannot be negative")
println("Valid age: $age")
}
fun main(args: Array<String>) {
try {
validateAge(-1)
} catch (e: IllegalArgumentException) {
println(e.message)
}
}
Sample Output
Age cannot be negative
When to use
Use try/catch when calling code that can fail — file access, network calls, parsing — and you need a controlled recovery path.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
The
println("Valid age: $age")statement writes a line to the console — this produces part of the sample output below. -
The
println(e.message)statement writes a line to the console — this produces part of the sample output below. -
Throw creates and raises an exception at runtime.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
- Understand the core idea: throw creates and raises an exception at runtime.
- Prefer readable names and small functions so examples map directly to real projects.
- Run and modify the example — change values and observe how the output changes.
Common Mistakes
- Skipping the example and only reading the definition — hands-on practice cements the concept.
- Copying syntax without understanding nullable vs non-nullable types or scope rules.
- Ignoring compiler warnings that often point to safer alternatives.
Key Points
- throw creates and raises an exception at runtime.
- It stops normal flow and transfers control to the nearest catch block.
- Use throw for invalid input or unexpected states.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Semicolons at the end of statements are optional in Kotlin.
Frequently Asked Questions
What is throw Exception in Kotlin?
When should I use throw Exception?
How is throw Exception different from Java?
How do I practice this topic?
Related Tutorials
finally Block in Kotlin
Learn finally Block in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialCustom Exception in Kotlin
Learn Custom Exception in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial