Level 1 – Reading Errors (≈ 10 minutes)
Concepts: compile‑time vs runtime errors, error messages, stack traces, print debugging vs debugger.
We will start with two small programs:
BuggyCalculator.java– has compile‑time and runtime issues.BuggyLoop.java– has a logic bug (off‑by‑one).
1. Setup the files
Create or open these files in OnlineGDB and paste in the code below.
BuggyCalculator.java
public class BuggyCalculator {
// Intentionally buggy method: sometimes crashes, sometimes returns wrong result
public static int divide(int a, int b) {
// BUG 1: possible division by zero (runtime error)
return a / b;
}
public static int sumFirstThree(int[] numbers) {
// BUG 2: potential ArrayIndexOutOfBoundsException
int total = 0;
for (int i = 0; i <= 3; i++) { // off‑by‑one AND assumes at least 4 elements
total += numbers[i];
}
return total;
}
}
BuggyLoop.java
public class BuggyLoop {
// Intentionally buggy method: tries to count from 1 to n
public static int countUpTo(int n) {
int count = 0;
// BUG 3: incorrect loop condition, never reaches n
for (int i = 1; i < n; i++) {
count++;
}
return count;
}
}
Main.java
Replace the contents of Main.java with:
public class Main {
public static void main(String[] args) {
// Example 1: division
System.out.println("Trying division...");
int result = BuggyCalculator.divide(10, 0);
System.out.println("Result: " + result);
// Example 2: sum first three
int[] nums = {1, 2};
System.out.println("Trying sumFirstThree...");
int sum = BuggyCalculator.sumFirstThree(nums);
System.out.println("Sum: " + sum);
// Example 3: loop
System.out.println("Trying countUpTo(5)...");
int count = BuggyLoop.countUpTo(5);
System.out.println("Count: " + count);
}
}
2. Run and observe
- Open
Main.java. - Click Run (not Debug yet).
- Watch the Console output and error messages.
Expected symptoms:
- The program should throw a runtime error (an exception) on the division line.
- You should see a stack trace in the console mentioning:
- The type of exception.
- The line number in
BuggyCalculator.java. - The line number in
Main.javawhere it was called.
If you fix that and run again, you will later see:
- Another exception from
sumFirstThree. - A logic bug:
countUpTo(5)prints the wrong count but does not crash.
3. Read the error messages
Focus on the first failure (division by zero):
- Look for the exception type (e.g.,
java.lang.ArithmeticException). - Find the message (e.g.,
/ by zero). - Find the topmost line from your code in the stack trace:
- It should show
BuggyCalculator.dividewith a line number. - It should show
Main.mainwith a line number.
- It should show
Answer (to yourself or a neighbor):
- Is this a compile‑time error or a runtime error? Why?
- Which line in which file is the “root cause” line?
4. Quick detective work (no debugger yet)
Without changing anything, ask:
- Why might
bbe zero individe? - Why might
sumFirstThreehit an out‑of‑bounds index?
Use print debugging:
- Add
System.out.println("a=" + a + ", b=" + b);at the start ofdivide. - Add
System.out.println("numbers length=" + numbers.length);at the start ofsumFirstThree. - Run again and see the printed values before the crash.
5. Fix the obvious runtime bugs
Make the program stop crashing, but don’t worry about perfect logic yet.
Suggested changes (try on your own first):
- In
divide, guard againstb == 0:- Print an error and return 0, or
- Throw a more descriptive exception.
- In
sumFirstThree, handle arrays with fewer than 3 elements.
Once you have fixed the crashes:
- Comment out or remove the temporary
printlncalls you added. - Run again and confirm there are no exceptions.
6. Find the logic bug
Now focus on BuggyLoop.countUpTo(5):
- What do you expect
countUpTo(5)to return? - What does it actually print?
- Is this a compile‑time error, runtime error, or logic bug?
You can:
- Add a
printlninside the loop to printieach time. - Or (in the next Level) use the debugger to step through it.
7. Wrap‑up questions (Level 1)
Discuss or write short answers:
- What is the difference between a compile‑time error and a runtime exception?
- How does the stack trace help you find the root cause?
- When is print debugging helpful, and when might it be slower than using a debugger?
You will revisit these same bugs in Level 2, but this time with breakpoints and stepping.