Room 6 of 15 · about 10 minutes

One line the compiler requires

The next room opens a file. Java refuses to compile a method that opens a file unless the method header carries one extra piece, so you get that piece here, on its own, before you need it.

Tasks checked0 of 2 XP earned on this path0

What this room checks you can do

Student can import java.io.FileNotFoundException, declare it with throws on every method that constructs a Scanner on a File, distinguish it from FileSystemNotFoundException in java.nio.file, and explain why a pre-check (e.g., exists() or canRead()) does not remove the declaration requirement.

Notes

FileNotFoundException is checkednamed here, worked later

The line you need

import java.io.FileNotFoundException;
public static String[] readLines(final String filename) throws FileNotFoundException

What is not in this room

Why the compiler asks for that line, and which other errors work the same way, is room 8. You do not need any of it to open a file. Copy the line for now.

Tasks

Do each one, then check the box. Checking a box is you saying you did it. You can uncheck a box if you check it by accident.

  1. trace
    Show the answer

    compile error of the form "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown."

  2. write
    Show the answer

    includes import java.io.File;, import java.io.FileNotFoundException;, import java.util.Scanner;, and the header public static String[] readLines(final String filename) throws FileNotFoundException.

Self check

Type what you think the answer is. Getting it wrong costs nothing and you can try as many times as you want.

Given two import lines import java.io.FileNotFoundException; versus import java.nio.file.FileSystemNotFoundException;, identify which one allows a method whose header declares throws FileNotFoundException to resolve.

Practice, untimed

Open this whenever you want, before the tasks or after them. Nothing in this section is recorded and nothing here is timed.

  1. Given a method that calls f.exists() and only constructs new Scanner(f) if the check passes, predict whether the throws FileNotFoundException clause is still required.trace
    Show the answer

    yes; the compiler does not analyze the runtime guard.

How this room finishes

This room is done when both tasks are checked and the self check is answered. It is a short one on purpose.