A called COBOL program should return to its caller. A main batch program should end the run unit. A paragraph used by PERFORM THRU may only need a named end point. Those are three different jobs, and using EXIT, EXIT PROGRAM, GOBACK, or STOP RUN in the wrong place can leave the next developer staring at confusing control flow.
What does EXIT mean in COBOL?
The plain EXIT. statement is mainly a common end point. IBM treats the simple form like CONTINUE, so control can pass through it when no active PERFORM range is returning from that point. It is not the same as ending a program.
The confusion comes from the word itself. In daily language, exit means leave. In COBOL, EXIT., EXIT PROGRAM, EXIT PARAGRAPH, and EXIT SECTION each have their own rules.
EXIT, EXIT PROGRAM, GOBACK, and STOP RUN
| Statement | Main use | What happens |
|---|---|---|
EXIT. |
Named end point | Acts like a no-operation point in the procedure flow. |
EXIT PROGRAM |
Called program return | Returns control to the statement after the active CALL. |
GOBACK |
Main or called program ending | Returns from a subprogram like EXIT PROGRAM; in a main program it ends like STOP RUN. |
STOP RUN |
End the run unit | Terminates the run unit and closes files for programs in that run unit. |
Plain EXIT example
Older COBOL often uses an exit paragraph at the end of a performed range. The paragraph gives PERFORM paragraph-a THRU paragraph-exit a clear final point.
PROCEDURE DIVISION.PERFORM 2000-VALIDATE-ORDERTHRU 2000-VALIDATE-EXITGOBACK.2000-VALIDATE-ORDER.IF ORDER-NUMBER = SPACESMOVE "Y" TO WS-ERROR-SWEND-IF.2000-VALIDATE-EXIT.EXIT.
The EXIT. paragraph does not return to a caller by itself. The active PERFORM controls the return. If some other path falls into that paragraph without an active performed range, control can pass to the next paragraph.
EXIT PROGRAM example
Use EXIT PROGRAM in a called program when it has finished and should return to the caller. This is common in reusable validation, formatting, and lookup modules.
IDENTIFICATION DIVISION.PROGRAM-ID. VALDCUST.PROCEDURE DIVISION USING LK-CUSTOMER-REC LK-RETURN-CODE.IF LK-CUSTOMER-ID = SPACESMOVE 8 TO LK-RETURN-CODEEXIT PROGRAMEND-IFMOVE 0 TO LK-RETURN-CODEEXIT PROGRAM.
When a CALL is active, EXIT PROGRAM returns to the statement after the call. In a main program with no active call, it should not be used as the normal ending statement.
GOBACK example
GOBACK is often used as a clean final statement because it works naturally in both main programs and called programs. In a called program, it returns to the caller. In a main program, it returns control to the system or caller of the main program.
PROCEDURE DIVISION.PERFORM 1000-OPEN-FILESPERFORM 2000-PROCESS-FILEPERFORM 9000-CLOSE-FILESGOBACK.
Many shops prefer GOBACK at the end of batch programs because it avoids changing the statement when the same program structure is reused as a subprogram.
STOP RUN example
STOP RUN ends the COBOL run unit. That makes it a poor choice inside a called utility program, because it can end more than the current program.
PROCEDURE DIVISION.IF WS-FATAL-ERRORMOVE 12 TO RETURN-CODESTOP RUNEND-IF.
Use STOP RUN only when the program really should terminate the run unit. For normal subprogram return, use EXIT PROGRAM or GOBACK.
EXIT PARAGRAPH and EXIT SECTION
Enterprise COBOL also supports procedure forms such as EXIT PARAGRAPH and EXIT SECTION. These statements leave the current paragraph or section without running the remaining statements in that paragraph or section. They are useful for clear early-exit logic when the coding standard permits them.
3000-CHECK-INPUT.IF INPUT-STATUS NOT = "00"MOVE 8 TO RETURN-CODEEXIT PARAGRAPHEND-IFPERFORM 3100-VALIDATE-FIELDS.
Do not hide business logic behind too many early exits. One or two clear exits can make code easier to read. A paragraph full of exit paths becomes hard to test.
Common mistakes
Using EXIT when EXIT PROGRAM is needed
EXIT. does not end a called program. If the program must return to the caller, use EXIT PROGRAM or GOBACK.
Using STOP RUN in a subprogram
A subprogram should usually return to its caller. STOP RUN can end the run unit, so it may close files and stop processing outside the subprogram.
Depending on PERFORM THRU everywhere
PERFORM THRU and exit paragraphs appear in many older systems, but they can make control flow harder to follow. For new code, a single performed paragraph or inline PERFORM is often easier to maintain.
Practical coding checklist
- Use
EXIT.only as a clear common end point. - Use
EXIT PROGRAMwhen a called program must return to the activeCALL. - Use
GOBACKwhen the program ending should work in both main and called contexts. - Use
STOP RUNonly when the whole run unit should end. - Keep end points easy to scan in the Procedure Division.
Related Mainframe Forum guides
For related control-flow topics, read COBOL PERFORM statement, COBOL STOP RUN statement, COBOL CONTINUE and NEXT SENTENCE, COBOL CALL statement, and COBOL ending and reentering.
External references
IBM documents the Enterprise COBOL EXIT statement, EXIT PROGRAM format, GOBACK behavior, and STOP RUN behavior.
FAQ
Does EXIT end a COBOL program?
No. Plain EXIT. is mainly a common end point. Use EXIT PROGRAM, GOBACK, or STOP RUN when program ending behavior is required.
What is the difference between EXIT and EXIT PROGRAM?
EXIT. marks a procedure point and acts like no operation. EXIT PROGRAM returns from a called program to the statement after the active CALL.
Should I use GOBACK or STOP RUN?
Use GOBACK when the program should return cleanly from either a main or called context. Use STOP RUN only when the run unit should end.
Is EXIT PARAGRAPH the same as EXIT PROGRAM?
No. EXIT PARAGRAPH leaves the current paragraph. EXIT PROGRAM leaves the called program and returns to the caller.
No comments:
Post a Comment