If you want to run some cleanup code when a procedure ends like

• Deallocating the allocated storage.

• You want to delete a temporary file

You can put that code into the ON-EXIT section of your procedure, and the code will run no matter how your procedure ends

Example :

dcl-proc Sampleproc;

dcl-s p pointer;

p = %alloc(10);

average = total / num;


dealloc p;  // This runs always even if the procedure crashes.



When your procedure returns a value, it can return a new value within the ON-EXIT section.

If a RETURN operation is not reached in the ON-EXIT section, the original return value is used

dcl-proc myproc;

dcl-pi *n packed(5);

doReturn ind const;


return 5;


if doReturn;

return 6;



x = myproc (*off);

doReturn is off, so x = 5

x = myproc (*on);

doReturn is on, so x = 6