Concepts / Program Exit Status

Program Exit Status

The phone book data is held in a TreeMap while the program runs, and modifications are made to that in-memory structure.

  • Programming

Why a Phone Book Needs a Saved File

A program that keeps information between runs needs a way to store that information outside its own running state. If a phone book forgot every entry as soon as it closed, it would be of little use. The phone book program in this section solves this with a data file.

The idea to hold on to through this article is that the program does not work on the file directly while the user is interacting with it. Instead, the phone book's data is kept in a TreeMap in memory, and the file comes into play at specific moments. Understanding when the file is touched, and what happens if touching it fails, is the goal of this lesson.

Where the Data Lives While the Program Runs

When the program starts, the phone book is held in a TreeMap. The user interacts with that in-memory structure rather than with the file. Every modification the user makes, such as adding or changing an entry, is made to the TreeMap while the program is running.

Because the TreeMap is where the work happens, the program can run from start to finish without the file being involved in every action. The file is a place the data goes at the end of the run, not a place that gets updated after every change. Keep this route in mind as you move through the rest of the article: changes go into memory, memory holds them while the program runs, and the file is involved at the end only under certain conditions.

What do you think happens?

A user opens the phone book, looks up one person's number, and then quits without adding, changing, or removing any entry. Does the program write the data file when it ends?

  • Yes, the file is always written at the end

  • No, the file is written only if changes were made during the run

Reveal answer
Answer:

No, the file is written only if changes were made during the run.

The source states that the data file is written when the program ends, and only if changes were made during the run. A lookup does not modify the TreeMap, so the write step is skipped.

When the Data File Is Written

The data file is written at the moment the program ends, but only under one condition: changes were made to the phone book during the run. This means the program has to make a decision at exit. It checks whether the in-memory TreeMap has been modified, and that check determines whether the file write happens at all.

Program endsChanges made thisrun?Write TreeMap to datafileProgram finishesSkip file writeyesno
Under which conditions does the phone book write to the file, and which path skips the write entirely?

The diagram shows the two paths. If changes were made, the TreeMap is written to the data file before the program finishes. If no changes were made, the write step is skipped and the program finishes without touching the file. A session that only reads entries therefore leaves the saved data exactly as it was.

What Happens When a File Operation Fails

File operations can fail. The program handles this with a try-catch block that catches IOException. When an IOException occurs while the program reads or writes the phone book data file, the catch block runs instead of the normal path. Control moves away from the regular steps, and the program does not carry on as though the file operation succeeded.

File read or writeIOException thrown?Normal path continuesCatch block runsPrint error and filenamePrint cannot-continuemessageSystem.exit(1)noyes
When a file write throws an IOException, where does control go, and how does that differ from a successful write?

The flowchart shows the two outcomes. The no branch leads to normal continuation. The yes branch runs the catch block, prints the messages, and ends with exit status 1. In the successful case the program keeps going; in the failure case the program stops at the catch block.

Worked example

Tracing a Failed File Write at Exit

The user has changed two entries during the run, so the program reaches the exit step and attempts to write the TreeMap to the data file. The write throws an IOException. What does the program print, and does it continue?

  1. 1
    Check for changes

    Changes were made, so the program proceeds to write the TreeMap to the data file.

  2. 2
    The write throws an IOException

    The file operation fails, and control moves to the try-catch block that catches IOException.

  3. 3
    Print the error and the file name

    The first two printed lines tell the user what failed and which file was involved, which is the information needed to find the problem.

  4. 4
    Print the cannot-continue message

    The third line explains that the program will not go on.

  5. 5
    Exit with status 1

    The call to System.exit(1) ends the program and reports status code 1 to whatever started it. Any steps after the failure do not run.

Result

The program prints the error message, the file name, and the cannot-continue message, then exits with status 1. The steps after the failed file operation are skipped.

Locating a Failure in the Sequence of Steps

A run of the phone book has a clear order: the program starts and holds entries in the TreeMap, the user makes changes, the user quits, and the program writes the file if changes were made. A file error can only occur during the file work, which in this program is the read or write step. The sequence diagram below places that step within the run.

UserPhone Book ProgramTreeMapData FileStart and use phone bookHold entries in memoryMake changesUpdate entriesQuitWrite file if changedIOExceptionPrint error, exit status 1
At which step in the run can a file error occur, and which steps come before and after it?

Read the diagram from top to bottom. The file failure appears only near the end, after the user has quit and the program has decided to write. Everything before it, the starting, the in-memory updates, and the quit, has already happened. Everything after it, the remaining logic, does not happen, because the catch block exits the program. So when a run ends with the phone book error message and status 1, you know the failure was in the file operation, not in earlier logic.

What do you think happens?

A run prints the phone book error message and exits with status 1. Which step in the sequence most likely failed?

  • The step where the user looked up a name during the run

  • The file read or write step during the file work

Reveal answer
Answer:

The file read or write step during the file work.

The source states that a program exiting with status 1 after printing the phone book error message has failed during the file operation, not later in the logic.

Mistakes When Reading a Failed Run

  • Common mistake

    Assuming the file is written after every change

    Why it fails

    The data file is written when the program ends, and only if changes were made during the run. Changes made during the session go into the TreeMap first.

    Do this instead

    Think of the file write as a single step at exit, gated by whether the TreeMap was changed.

  • Common mistake

    Expecting the program to keep running after a file error

    Why it fails

    The catch block exits the program with status code 1, so steps after the failure do not run.

    Do this instead

    When an IOException is caught, expect the output to end with the cannot-continue message and the exit.

  • Common mistake

    Looking for the failure in the logic that ran after the file step

    Why it fails

    A program exiting with status 1 after printing the phone book error message has failed during the file operation, not later in the logic.

    Do this instead

    Start the search at the file read or write step and work backward through the run.

  • Common mistake

    Ignoring the file name in the error output

    Why it fails

    The first two printed lines identify what failed and where, which is the information needed to find the problem.

    Do this instead

    Use the file name and error text from the output to locate the failing operation.

Tracing a Run Yourself

MEDIUM

A user starts the phone book, adds one new entry, and then quits. During the exit step, the file write throws an IOException. Trace the run: list which steps happened, which step failed, which steps were skipped, and what the final output and exit status are.

Hints
  • Start by asking whether the entry added during the session was a change, since that determines whether the file write is attempted.
  • Once the IOException occurs, the catch block runs instead of the normal path.
  • The final exit status for this path is 1, and the messages printed are the error, the file name, and the cannot-continue message.
EASY

Compare two runs. In Run A, the user only looks up entries and quits. In Run B, the user changes one entry and quits, and the file write throws an IOException. Which run prints the cannot-continue message, and which run leaves the saved data file untouched? Explain your reasoning using the exit decision.

Hints
  • Run A makes no changes, so consider whether the write step is reached.
  • Run B reaches the write step, and the failure then leads to the catch block.

Key Takeaways

  • The phone book's data is held in a TreeMap while the program runs, and user modifications are made to that in-memory structure.
  • The data file is written when the program ends, and only if changes were made during the run; a session with no changes skips the write.
  • An IOException during file work is caught by a try-catch block that prints the error, the file name, and a message that the program cannot continue, then exits with status code 1.
  • Because the exit with status 1 happens immediately, steps after the failed file operation do not run, so a status 1 exit after the phone book error message points to the file operation as the source of failure.