Share
Explore

Lab: Using the IntelliJ Debugger

Here's a simple yet clear Kotlin program that demonstrates method calls nested within each other, which is perfect for:
🧠 Understanding the call stack. We know from the von Neumann Memory Model that the stack memory is where code (methods) live. The Heap memory is where data variables live.
🐞 Seeing the stack trace in the IntelliJ debugger

🧪 CallStackDemo.kt – Method Call Chain Example

fun main() {
println("🟢 Starting main...")
firstMethod()
println("🟢 Back in main, all done.")
}

fun firstMethod() {
println("🔵 Entered firstMethod")
secondMethod()
println("🔵 Exiting firstMethod")
}

fun secondMethod() {
println("🟡 Entered secondMethod")
thirdMethod()
println("🟡 Exiting secondMethod")
}

fun thirdMethod() {
println("🔴 Entered thirdMethod")

// Intentionally trigger an error to view the full stack trace
val result = 10 / 0 // 🚨 Will throw an ArithmeticException

println("🔴 This won't print because of the exception above")
}

✅ How to Use This in IntelliJ

Set breakpoints at:
The call to firstMethod() in main
Inside each method (e.g., first line of secondMethod, thirdMethod)
Run in Debug Mode (Shift+F9)
When the program hits a breakpoint:
Open the "Debugger" panel
Observe the stack frames in the call stack (you'll see main → firstMethod → secondMethod → thirdMethod)
When the exception is thrown, IntelliJ will show you the full stack trace, pinpointing where the error occurred and which chain of method calls led there.

🧠 What This Demonstrates

How control flows down through nested method calls
How the call stack builds up
How exceptions "bubble up" through the stack
How the debugger reflects this hierarchy
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.