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;

on-exit;

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

end-proc;

 

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;

end-pi;

return 5;

on-exit;

if doReturn;

return 6;

endif;

end-proc

x = myproc (*off);

doReturn is off, so x = 5

x = myproc (*on);

doReturn is on, so x = 6