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.
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.
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:
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:
dcl-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.
IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd 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 in the words of the most interesting man in the world: Stay thirsty my friend.
Turnover Pre-Run and Post-Run Promotion Form Commands
Developerworks Connections Sunset – How to Extend RDi
Why use IBM i RDi?
How to Install IBM Access Client Solutions (ACS)
5733XJ1 IBM i Access Client Solutions – QuickStartGuide
Install LANSA AXES – Automatic Web Interface for IBM i (AS/400) 5250 Applications
How to get a list of all files in an IFS folder
Edit UIM MENU with IBM i RDI
Encrypt IBM i File (Table) Data with no RPGLE changes using SQL