Java Arena
Step 1 of 4

Reading a crash

Programs go wrong in two ways. You've met the first since module 1: javac finds a mistake, and the program doesn't run at all. The second kind happens while the program runs: it gets to a line it can't carry out, and it stops. This is a crash, and Java calls the problem an exception.

This program computes an average, but nothing has been added yet:

public class Main {
    public static void main(String[] args) {
        int total = 0;
        int count = 0;
        System.out.println("Scores added: " + count);
        int average = total / count;
        System.out.println("Average: " + average);
    }
}
Output
Scores added: 0
The program crashes with
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.main(Main.java:6)

A crash message has three parts worth reading:

  • What happened: the exception's name, here ArithmeticException, a calculation that can't be done.
  • Why: the message after the colon, / by zero. A whole number can't be divided by zero.
  • Where: at Main.main(Main.java:6), the method main on line 6.

Everything printed before the crash still appears: Scores added: 0 came out, and the lines after line 6 never ran.

When the crash happens inside a method, Java lists every method that was running, the one that crashed first. This is the call stack from module 11:

public class Main {
    public static void main(String[] args) {
        printShare(12, 4);
        printShare(12, 0);
    }

    public static void printShare(int total, int people) {
        System.out.println("Each gets " + share(total, people));
    }

    public static int share(int total, int people) {
        return total / people;
    }
}
Output
Each gets 3
The program crashes with
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.share(Main.java:12)
	at Main.printShare(Main.java:8)
	at Main.main(Main.java:4)

Read it from the top: the division on line 12 in share failed. share was called by printShare on line 8, and that call came from line 4 of main, the second printShare. The first call worked; only the call with 0 people crashed.

Java Arena shows a crash the same way and explains it in plain words, with the line it happened on. The fix is often not on the crashing line itself: ask where the bad value came from.

Your task

Challenge 1 of 3

This program reads scores until the user types 0, then prints their average. It crashes when the very first input is 0. Make it print No scores in that case, and the average otherwise. With the input 0 it prints:

No scores

Challenge 2

This program prints how many points each team member gets. It crashes when a team has no members. Change only the method perMember, so it returns 0 when members is 0. The check runs printPoints(30, 0); and expects:

Each member gets 0

Challenge 3

Here is a crash from a program you can't see:

The program crashes with
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.average(Main.java:14)
	at Main.report(Main.java:9)
	at Main.main(Main.java:4)

Fill in the blanks: the method the crash happened in, and the line of main that started the call.