.st0{fill:#FFFFFF;}

RPG ON-EXIT is housekeeping for sub-procedures 

 May 15, 2017

By  NickLitten

IBM is adding all kinds of new tweaks to RPG with each new release of IBM i. RPG ON-EXIT is a great example of a neat tweak to the RPG programming language.

Last year, IBM quietly introduced a rather neat new function called “ON-EXIT” to our sub procedures in RPG.

RPG ON-EXIT is a nice structured way of storing procedure house keeping and/or error catching.

ON-EXIT is available if you are running IBM i V7.2 or higher:

The ILE RPG compiler is enhanced with a new ON-EXIT opcode which begins the “ON-EXIT” section containing code to be run whenever a procedure ends, either normally or abnormally.

So, if you have code that you always want to run at the end of a procedure you can just put that in the ON-EXIT section.

So how does ON-EXIT work?

The ON-EXIT operation code begins the ON-EXIT section. The ON-EXIT section contains code that runs every time that the procedure ends, whether it ends normally or abnormally. The ON-EXIT section runs under the following conditions:

  • The procedure reaches the end of the main part of the procedure.
  • The procedure reaches a RETURN operation.
  • The procedure ends with an unhandled exception.
  • The procedure is canceled, due to the end of the job or subsystem, or due to an exception message being sent to a procedure higher in the call stack.

By placing your clean-up code, such as deleting temporary files and deallocating heap storage, in the ON-EXIT section, you ensure that it is always run, even if your procedure ends with an unhandled exception, or if it is canceled.

Extended Factor 2 contains an optional indicator variable that indicates whether the procedure ended normally or abnormally. If the procedure returned normally, the indicator is set to *OFF. If the procedure ended with an unhandled exception or if the procedure was canceled for any other reason, the indicator is set to *ON.

The ON-EXIT section is coded at the end of the subprocedure, following any subroutines.

All variables and files defined in the procedure are available in the ON-EXIT section.

I can already think about using ON-EXIT something like this:

RPG ON-EXITdcl-proc myproc;
 dcl-s OhBuggerIcrashed ind inz(*off);
 dcl-s myprocFinished ind inz(*off);
 Open fileName;
 doStuff = things * widgets / shoeshize;
 filename = crtTempFile();
 ... // do lots of subprocedure logic stuff
 myprocFinished = *on;

 return; 
on-exit OhBuggerIcrashed;

 // "OhBuggerIcrashed" is set *ON if the on-exit is invoked abnormally
 // (so it acts sort of like a *PSSR capture)
 if OhBuggerIcrashed;
   reportProblem ();
 endif;

 // then we could have some logic added around the "myProcFinished" indicator
 if myprocFinished;
   dltTempFile (filename);
 endif;

 // any housekeeping that we want to do if we finished cleanly or not
 Close fileName; 

end-proc;

This is discussed in more detail over at the IBM Developerworks.

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

Iā€™m always looking for awesome input, feedback and critique!

>