FILE STATUS and Handling Errors

COBOL COBOL 2014 (ISO/IEC 1989:2014), built with GnuCOBOL 3.x · ✓ verified by execution on 2026-08-16

If you have written any COBOL code that touches a file, you know that things can go wrong. What happens if you try to OPEN a file that does not exist? What if you do not have permission to read it?

In COBOL, you handle these scenarios by checking the FILE STATUS.

When you declare a FILE STATUS variable, the COBOL runtime will automatically update it after every input/output operation (OPEN, READ, WRITE, CLOSE). By checking this two-character alphanumeric variable, you can gracefully handle errors instead of having your program crash in the middle of the night.

COBOL Operation Runtime Updates code 00 WS-FS

Declaring a File Status Variable

To use FILE STATUS, you must explicitly declare it in two places:

  1. In the FILE-CONTROL paragraph, using FILE STATUS IS.
  2. In the WORKING-STORAGE SECTION, as a two-character alphanumeric item (PIC XX).

The standard strictly requires the variable to be alphanumeric, even though the status codes look like numbers.

cobol ✓ verified output
       ID DIVISION.
       PROGRAM-ID. EX01.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT F ASSIGN 'demo1.txt' ORGANIZATION LINE SEQUENTIAL
           FILE STATUS IS WS-FS.
       DATA DIVISION.
       FILE SECTION.
       FD F.
       01 R PIC X.
       WORKING-STORAGE SECTION.
       01 WS-FS PIC XX.
       PROCEDURE DIVISION.
           OPEN OUTPUT F.
           DISPLAY 'OPEN OUTPUT STATUS: ' WS-FS.
           CLOSE F.
           DISPLAY 'CLOSE STATUS: ' WS-FS.
           STOP RUN.
Output
OPEN OUTPUT STATUS: 00
CLOSE STATUS: 00

If the operation is successful, the variable will contain '00'. Above, both our OPEN and CLOSE operations completed flawlessly, so they both logged '00'.

What Happens When Things Go Wrong?

One of the most common errors in data processing is attempting to open a file that simply isn’t there. If you don’t check for this, your program might abort unexpectedly.

Let’s see what happens if we attempt to open a file that does not exist. Read the code below and predict what the WS-FS variable will contain after the OPEN INPUT statement executes.

Predict the output cobol

Read the code. What exactly will it print? Commit to an answer before you look.

       ID DIVISION.
       PROGRAM-ID. EX02.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL. SELECT F ASSIGN 'no.txt'
           ORGANIZATION LINE SEQUENTIAL FILE STATUS WS-FS.
       DATA DIVISION.
       FILE SECTION.
       FD F. 01 R PIC X.
       WORKING-STORAGE SECTION.
       01 WS-FS PIC XX.
       PROCEDURE DIVISION.
           OPEN INPUT F.
           DISPLAY 'OPEN INPUT STATUS: ' WS-FS.
           STOP RUN.
Output
OPEN INPUT STATUS: 35

A status code of '35' means the file was not found. If we check for this status right after our OPEN statement, we can display a helpful error message or create the file, rather than crashing.

Not All Non-Zero Codes Are Errors!

A common misconception is that any FILE STATUS other than '00' means a fatal error has occurred. This is false! Some status codes indicate expected lifecycle events.

For example, when you are reading a file sequentially, you will eventually reach the end. When this happens, the FILE STATUS becomes '10'.

cobol ✓ verified output
       ID DIVISION.
       PROGRAM-ID. EX03.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT F ASSIGN 'demo3.txt' ORGANIZATION LINE SEQUENTIAL
           FILE STATUS IS WS-FS.
       DATA DIVISION.
       FILE SECTION.
       FD F.
       01 R PIC X.
       WORKING-STORAGE SECTION.
       01 WS-FS PIC XX.
       PROCEDURE DIVISION.
           OPEN OUTPUT F. MOVE 'A' TO R. WRITE R. CLOSE F.
           OPEN INPUT F.
           READ F.
           DISPLAY 'READ 1 STATUS: ' WS-FS.
           READ F.
           DISPLAY 'READ 2 STATUS: ' WS-FS.
           CLOSE F.
           STOP RUN.
Output
READ 1 STATUS: 00
READ 2 STATUS: 10

As you can see, the first read was successful ('00'), but the second read hit the end of the file, giving us the '10' status code. This is why a READ loop continues until it catches this specific code (often internally handled by the AT END phrase).

Using 88-Level Conditions for Cleaner Code

Because status codes are cryptic, seasoned COBOL developers use 88-level condition names in WORKING-STORAGE to make their PROCEDURE DIVISION highly readable. Instead of writing IF WS-FS = '35', you can define a descriptive name like FS-MISSING.

cobol ✓ verified output
       ID DIVISION.
       PROGRAM-ID. EX05.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT F ASSIGN 'demo5.txt' ORGANIZATION LINE SEQUENTIAL
           FILE STATUS IS WS-FS.
       DATA DIVISION.
       FILE SECTION.
       FD F.
       01 R PIC X.
       WORKING-STORAGE SECTION.
       01 WS-FS PIC XX.
          88 FS-OK VALUE '00'.
          88 FS-MISSING VALUE '35'.
       PROCEDURE DIVISION.
           OPEN INPUT F.
           IF FS-MISSING
               DISPLAY 'File is missing as expected.'
           END-IF.
           STOP RUN.
Output
File is missing as expected.

Other Common Codes

File status codes are grouped by their first digit. Here are some you will encounter frequently:

For instance, attempting to close a file that was never opened results in a '42'!

cobol ✓ verified output
       ID DIVISION.
       PROGRAM-ID. EX06.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT F ASSIGN 'demo6.txt' ORGANIZATION LINE SEQUENTIAL
           FILE STATUS IS WS-FS.
       DATA DIVISION.
       FILE SECTION.
       FD F.
       01 R PIC X.
       WORKING-STORAGE SECTION.
       01 WS-FS PIC XX.
       PROCEDURE DIVISION.
           CLOSE F.
           DISPLAY 'CLOSE STATUS WITHOUT OPEN: ' WS-FS.
           STOP RUN.
Output
CLOSE STATUS WITHOUT OPEN: 42

Relying on FILE STATUS makes your COBOL programs incredibly resilient. Never assume a file operation worked—always check the status!

Check yourself

Which statement accurately describes how FILE STATUS should be declared in COBOL?

Reveal answer

It must be declared as an alphanumeric PIC XX item, even though codes resemble numbers. — The COBOL standard specifies that FILE STATUS must be an alphanumeric field (PIC XX). It must be explicitly declared in both the SELECT clause and WORKING-STORAGE.

If your FILE STATUS variable contains the value '10', what does this mean?

Reveal answer

The end of the file was reached during a sequential read. — Status '10' indicates End of File. It is a normal, expected non-zero code during sequential reading operations.

Why is relying solely on the AT END phrase insufficient for robust file handling?

Reveal answer

AT END only catches EOF (code 10), but ignores missing files (35) or permission errors (37). — AT END only handles the End of File condition. Without FILE STATUS, other errors like 'File Not Found' (35) will crash your program unpredictably.

When does the FILE STATUS variable update its value?

Reveal answer

After every file input/output operation. — The FILE STATUS variable is updated after every single file operation (OPEN, READ, WRITE, CLOSE, etc.), reflecting its outcome.

Challenges

Challenge 1 +50 XP

Write a program that attempts to `OPEN INPUT` a file called `missing.txt`, checks the file status, and displays `File not found!` if the status is 35, or `Unexpected status` otherwise.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "File not found!\n"
Show solution (0 XP)
ID DIVISION.
PROGRAM-ID. CHAL1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT F ASSIGN 'missing.txt' ORGANIZATION LINE SEQUENTIAL
    FILE STATUS IS WS-FS.
DATA DIVISION.
FILE SECTION.
FD F.
01 R PIC X.
WORKING-STORAGE SECTION.
01 WS-FS PIC XX.
PROCEDURE DIVISION.
    OPEN INPUT F.
    IF WS-FS = '35'
        DISPLAY 'File not found!'
    ELSE
        DISPLAY 'Unexpected status'
    END-IF.
    STOP RUN.

Challenge 2 +50 XP

Write a loop that reads `demo.txt` and stops when the file status becomes '10'. Display the file status after each read operation.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "Status: 00\nStatus: 10\n"
Show solution (0 XP)
ID DIVISION.
PROGRAM-ID. CHAL2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT IN-FILE ASSIGN 'demo.txt' ORGANIZATION LINE SEQUENTIAL
    FILE STATUS IS WS-FS.
DATA DIVISION.
FILE SECTION.
FD IN-FILE.
01 R PIC X.
WORKING-STORAGE SECTION.
01 WS-FS PIC XX.
PROCEDURE DIVISION.
    OPEN OUTPUT IN-FILE. MOVE 'A' TO R. WRITE R. CLOSE IN-FILE.
    OPEN INPUT IN-FILE.
    PERFORM UNTIL WS-FS = '10'
        READ IN-FILE
        DISPLAY 'Status: ' WS-FS
    END-PERFORM.
    CLOSE IN-FILE.
    STOP RUN.

🐞 Bug Hunt +25 XP

This program is supposed to gracefully detect if `missing.txt` does not exist by checking if `WS-FS` becomes '35'. But the developer forgot to link the status variable! Run the code, and observe how GnuCOBOL aborts with an unhandled file error because the status has nowhere to go. Fix the `SELECT` statement so the variable captures the error gracefully.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "Gracefully handled: File not found.\n"
Need a hint? (−25% XP)

Add `FILE STATUS IS WS-FS` to the end of the `SELECT` statement in `FILE-CONTROL`.

Show solution (0 XP)
ID DIVISION.
PROGRAM-ID. BUG1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT F ASSIGN 'missing.txt' ORGANIZATION LINE SEQUENTIAL
    FILE STATUS IS WS-FS.
DATA DIVISION.
FILE SECTION.
FD F.
01 R PIC X.
WORKING-STORAGE SECTION.
01 WS-FS PIC XX VALUE '00'.
PROCEDURE DIVISION.
    OPEN INPUT F.
    IF WS-FS = '35'
        DISPLAY 'Gracefully handled: File not found.'
    ELSE
        DISPLAY 'Unexpected status: ' WS-FS
    END-IF.
    STOP RUN.