Reading Stack Traces
Learn how to read stack traces to debug Java programs effectively.
Reading Stack Traces is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
When an exception is not caught, Java prints a stack trace. It shows the error type and the chain of method calls that led to it.
Stack trace format
A stack trace usually has:
- First line: exception type and message.
- Following lines: methods and file:line where the error traveled.
Code: uncaught exception
This uncaught NullPointerException will print a stack trace showing the path through methodA → methodB → main.
public class Main {
static void methodA() {
methodB();
}
static void methodB() {
String s = null;
System.out.println(s.length()); // NullPointerException
}
public static void main(String[] args) {
methodA();
}
}
Reading lines
Each stack trace line has the class, method, and source line number. Reading from top to bottom shows where the error happened and how it propagated.
Code: out of bounds
This prints ArrayIndexOutOfBoundsException with the exact line number, helping you locate the bug.
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
System.out.println(nums[5]); // ArrayIndexOutOfBoundsException
}
}
Code: printStackTrace
You can also call printStackTrace() inside catch to show the trace for debugging while continuing execution.
public class Main {
static void risky() {
throw new RuntimeException("Custom message");
}
public static void main(String[] args) {
try {
risky();
} catch (RuntimeException e) {
e.printStackTrace(); // print custom trace
}
}
}
Stack trace check
Quick check: What does the top line of a stack trace usually show?
Recap
Recap: Stack traces show exception type and the method call path. Reading them helps debug quickly by locating the error line.
Frequently asked questions
Is the “Reading Stack Traces” lesson free?
Yes — the full text of “Reading Stack Traces” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading Stack Traces”?
Learn how to read stack traces to debug Java programs effectively. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Reading Stack Traces” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- try/catch/finally
- Checked vs Unchecked
- Reading Stack Traces