Loading...

Java Throwing

Write the stop yourself with throw. Open Signal.java and throw IllegalArgumentException with the message empty before any print of water. Compile with javac Signal.java and run java Signal. The program stops on that throw line; water does not print. A second demo uses cups at 0 so an if enters and throws. A third demo uses cups at 3 so the if is skipped and the screen prints water. Leave reading files for the next page.

You write the stop

Earlier pages showed a stop that appeared because a line broke a rule — a bad array index threw ArrayIndexOutOfBoundsException. This page is the other direction. You choose the exception type and you write throw so the stop happens on purpose. Open a teaching file named Signal.java. Keep the public class name Signal so the file name matches. Delete leftover Guard catch demos, leftover Alarm array stops, and any Scanner or nextLine. The program must not wait for typing. This page needs no import line.
Inside main, throw before any quiet success print:
throw new IllegalArgumentException("empty");
Then keep a print that should not run when the throw fires:
System.out.println("water");
The words in quotes are the message. The type name is IllegalArgumentException. You are not dividing by zero and you are not reading slots[5]. The stop is there because you wrote throw.

Signal bare throw stops before water

java
public class Signal {
    public static void main(String[] args) {
        throw new IllegalArgumentException("empty");
        // water would not run after throw
    }
}

From the folder that holds Signal.java, run javac Signal.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Signal — not java Signal.java. Leave the .java ending off the run command.
The run stops on the throw line. The terminal names IllegalArgumentException and shows the message empty. A print of water after that throw does not run, because the program already stopped. If you keep an unreachable System.out.println("water"); after the throw inside the same main, some compilers warn that the print is unreachable; the teaching idea stays the same: after throw, normal lines in that path do not run. Prefer the bare throw file without a dead print if your javac complains about unreachable code — the stop itself is the lesson.
If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call. If you see blocked then after, you are still running a Guard catch file — switch to java Signal. If you see ArrayIndexOutOfBoundsException from slots[5], an Alarm leftover is still active; replace it with the throw line for this lesson.

Zero cups enters the if and throws

A second program starts fresh. Store cups as 0. If cups is less than 1, throw the same IllegalArgumentException with message empty. Keep a print of water after the if.

Signal zero cups throws

java
public class Signal {
    public static void main(String[] args) {
        int cups = 0;
        if (cups < 1) {
            throw new IllegalArgumentException("empty");
        }
        System.out.println("water");
    }
}

Compile with javac Signal.java, then run java Signal. Zero is less than one, so the if is entered and throw runs. The screen shows the same stop: IllegalArgumentException and empty. The word water is not printed. Nothing on this page catches the stop, so the program ends there. That is intentional — last page practiced catch; this page practices writing the throw.
Walk the braces once. The throw line sits inside the if block. The water print sits outside the if. Once throw runs, the program has already stopped, so water is never reached. The indent and braces are what put the throw inside the check.

Three cups skips throw and prints water

A third program starts fresh too. Set cups to 3. Keep the same if and the same throw and the same water print.

Signal three cups prints water

java
public class Signal {
    public static void main(String[] args) {
        int cups = 3;
        if (cups < 1) {
            throw new IllegalArgumentException("empty");
        }
        System.out.println("water");
    }
}

Compile and run again with javac Signal.java and java Signal. Three is not less than one, so the if is not entered. The throw never runs. The screen prints water. There is no stop. That contrast is the checkpoint: the throw only fires when your condition says the value is not usable.
Do not invent a catch around these teaching files on this page. You already practiced catch on Guard. Here you need to see the stop you wrote, and to see the path that skips the throw. Also do not replace IllegalArgumentException with ArrayIndexOutOfBoundsException for empty cups — that name belonged to a bad array index, not to a value check you authored with throw.

The name you wrote

The type you threw is IllegalArgumentException, not ArrayIndexOutOfBoundsException and not a bare word empty as the type. The message empty is what you chose to show next to the name. If you catch this stop later, the catch clause must name IllegalArgumentException. A catch for NullPointerException would not handle it. That catch practice is not required on these Signal files today.
IllegalArgumentException is a run-time exception in common beginner use, so these small main demos do not need a throws clause on main. Stay with this one clear type and message. You do not need a catalog of every throwable class before the next page opens a file.

Compared with catch on Guard

On the try-catch page, java Guard handled a bad index and continued to after. On this page, java Signal causes a stop on purpose and does not catch it. The array story is history. Keep Guard files only if they still sit nearby as older demos; run java Signal for the throw demos so the screen matches this lesson.

What this leaves for files

This page only shows you causing a stop with throw. The next page begins reading a file. Stay with writing the stop and skipping it when cups is usable. Do not open a file as a requirement on these Signal demos yet.

Before files

You can throw IllegalArgumentException("empty") and see the run stop before water. With cups at 0, the if enters and the same stop happens. With cups at 3, throw never runs and the screen prints water. Compile Signal.java and run java Signal for each kept demo in turn. The name of this stop is IllegalArgumentException. The next page is reading a file.
Keep the bare throw for a clear stop you wrote. Keep the cups pair for a condition that can skip the throw. Keep file reading off this page. Those three habits are the checkpoint before you move on. When water appears only on the cups-at-three run of java Signal, throwing did its job for this lesson and you are ready for the next topic about files.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top