Final challenge: break it, read it, fix it
After this: Predict which error a program raises, find the failing line, catch an error and fix a loop.
This challenge has five steps, and each step is scored on its own. You will say which error a program raises, find the failing line and function from a full message, complete the line that catches an error, say what an off-by-one loop prints, and say when catching an error is the right thing to do. Every step shows the code in Python and in Java; the names of the errors differ between the languages, and each step shows the one for your language. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Which error
Which error
A function divides by its input, and the program calls it with 0. What does this show?
def half(n): return 100 / n
print("Begin")print(half(0))public class Main { static int half(int n) { return 100 / n; }
public static void main(String[] args) { System.out.println("Begin"); System.out.println(half(0)); }}Find the line
Read the message. On which line did the program fail, inside which function, and which call handed over the bad value?
def share(total, people): return total / people
def bill(total): print(share(total, 0))
bill(60)Output
Traceback (most recent call last):
File "main.py", line 7, in <module>
bill(60)
~~~~^^^^
File "main.py", line 5, in bill
print(share(total, 0))
~~~~~^^^^^^^^^^
File "main.py", line 2, in share
return total / people
~~~~~~^~~~~~~~
ZeroDivisionError: division by zeropublic class Main { static int share(int total, int people) { return total / people; }
static void bill(int total) { System.out.println(share(total, 0)); }
public static void main(String[] args) { bill(60); }}Output
Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.share(Main.java:3) at Main.bill(Main.java:7) at Main.main(Main.java:11)
Catch it
Position 5 does not exist in a list of two. Complete the missing line so that the error is caught and the program prints No such position and then Done. Write the whole line.
names = ["Ana", "Bo"]try: print(names[5])____________________ # ← catch the position error print("No such position")print("Done")import java.util.ArrayList;import java.util.List;
public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(List.of("Ana", "Bo")); try { System.out.println(names.get(5)); } ____________________ // ← catch the position error System.out.println("No such position"); } System.out.println("Done"); }}Off by one
The same three prices, a different loop line. What does this print?
prices = [4, 5, 6]total = 0for i in range(len(prices) - 1): total = total + prices[i]print(total)import java.util.ArrayList;import java.util.List;
public class Main { public static void main(String[] args) { ArrayList<Integer> prices = new ArrayList<>(List.of(4, 5, 6)); int total = 0; for (int i = 0; i < prices.size() - 1; i++) { total = total + prices.get(i); } System.out.println(total); }}When to catch
A program asks a person to type their age and turns the text into a number with intInteger.parseInt. The person may type letters by mistake. Which is the right way to handle that?
- Predict which error a program raises, find the failing line and function, catch one kind of error, say what an off-by-one loop prints, and say when catching is right.