• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Data Structures Make Good Status Parameters

    October 31, 2012 Ted Holt

    When a good program goes bad, someone must fix it. Sometimes that someone is me. As for the “when,” it’s never a good time. It’s important to me that the failing program give me as much information as possible to help me pinpoint the cause of the error as quickly as possible.

    There are two ways a program can inform its caller that it could not complete normally. One way is by sending a message. (I have written about this topic before; see the Related Stories at the end of this article.) The other way is to use a status parameter, also known as a “return code.”

    It is common for a program to place a zero into a status parameter if all went well, and a non-zero value if something went wrong. A good example of this convention is the SQL state.

    I wrote earlier this year about an extension of the SQL state that identifies the statement that ended in error.

    You can take this idea even further, making the program in error pass back as much data as you wish. I illustrate with an RPG data structure, but the technique works with any language.

    First, create a message, like this:

    crtmsgf *curlib/mymsgf
    addmsgd msgid(MYM1101) msgf(*curlib/mymsgf) +
              msg('&2 errors were found in program &1.') +
              seclvl('Correct the errors and rerun.') +
              fmt((*char 10) (*bin 4))
    

    The message has two positional parameters, which occupy 14 bytes of storage.

    Now, define a compatible data structure in the program(s) that will trap for this error. Here’s the structure in RPG.

    D Status          ds           256    inz
    D   MsgID                        7a
    D   PgmName                     10a
    D   ErrorCount                  10i 0
    

    The first seven bytes are set aside for the message ID. The remaining subfields of the data structure match the definitions of the substitution values in the message description.

    I’ve kept the structure simple. It tells me the name of the rogue program and the number of errors it found. The more information you put into the structure, the better, because a user can read a message to you over the telephone, but having someone read a program dump to you is impractical.

    Loading the structure is easy.

         if ErrorCounter > *zero;
            MsgID = 'MYM1101';
            PgmName = ProcName;          // from the PSDS
            ErrorCount = ErrorCounter; 
            ouStatus = Status;
         endif;
    

    The last line gives the data back to the caller, because ouStatus is the first parameter.

    D OnePgm          pi
    D  ouStatus                    256a
    D  inWhs                         3a   const
    D  inItem                       12a   const
    

    The driver CL looks like this (without appropriate code to handle unexpected errors).

    pgm parm(&inOption &inItem &inWhs)
    
       dcl  &inOption    *char    1
       dcl  &inItem      *char   12
       dcl  &inWhs       *char    3
    
       dcl  &Status      *char  256
    
       call OnePgm  (&Status &inWhs &inItem)
    
       if cond(%sst(&Status 1 7) *eq ' ') then( +
             call TwoPgm  (&Status &inOption))
    
       if cond(%sst(&Status 1 7) *eq ' ') then(+
             call RedPgm  (&Status))
    
       if cond(%sst(&Status 1 7) *eq ' ') then(+
             call BluePgm  (&Status &inWhs))
    
       if cond(%sst(&Status 1 7) *ne ' ') then( +
             sndpgmmsg   msgid(%sst(&Status 1 7)) msgf(MyMsgF) +
                            msgdta(%sst(&Status 8 249)) +
                            msgtype(*escape))
    
    endpgm
    

    If a program finds an error, the system ignores the subsequent program calls. If any program ends in error, the last IF makes the CL program shut down by sending an escape message similar to this one:

    50 errors were found in program OnePgm.
    

    I used this technique in a project I worked on recently, and was very pleased with the results. Data structures make tremendous status parameters.

    RELATED STORIES

    Sending Escape Messages from RPG

    RPG Subprocedure Error-Handling with APIs

    Error Checking and Embedded SQL



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    Lightedge

    Your IBM Power Cloud Strategy Should Start With “Why?”

    Moving IBM i to the cloud isn’t just an infrastructure decision.

    A hardware refresh, data center exit, IBM i skills gap, disaster recovery requirement, or broader cloud initiative can each lead to a very different architecture.

    At Lightedge, we start by understanding what you’re trying to accomplish. Then we look at the workload, application dependencies, resiliency requirements, operating model, and long-term strategy to determine where that workload belongs.

    Managed IBM Power cloud? IBM Power Virtual Server? DR in the cloud? A hybrid architecture connecting IBM Power with AWS or Azure?

    There isn’t one right answer for every IBM i environment.

    Start with the business requirement. Understand the workload. Then decide where it belongs.

    Explore IBM Power cloud, managed services, and hybrid solutions from Lightedge.

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Sponsored Links

    Sirius Computer Solutions:  A comprehensive, cost-effective cloud solution for IBM i users
    BCD:  FREE Webinar: Making the Business Case for Web Enabling Your Green Screens. Nov 1
    ITJ Bookstore:  Bookstore BLOWOUT!! Up to 50% off all titles! Everything must go! Shop NOW

    IT Jungle Store Top Book Picks

    Bookstore Blowout! Up to 50% off all titles!

    The iSeries Express Web Implementer's Guide: Save 50%, Sale Price $29.50
    The iSeries Pocket Database Guide: Save 50%, Sale Price $29.50
    Easy Steps to Internet Programming for the System i: Save 50%, Sale Price $24.97
    The iSeries Pocket WebFacing Primer: Save 50%, Sale Price $19.50
    Migrating to WebSphere Express for iSeries: Save 50%, Sale Price $24.50
    Getting Started with WebSphere Express for iSeries: Save 50%, Sale Price $24.50
    The All-Everything Operating System: Save 50%, Sale Price $17.50
    The Best Joomla! Tutorial Ever!: Save 50%, Sale Price $9.98

    Running IBM i Access 7.1 and Windows 8 Changing Sub Tree Authorities In An IFS Folder

    Leave a ReplyCancel reply

Volume 12, Number 26 -- October 31, 2012
THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
Adsero Optima

Table of Contents

  • Running IBM i Access 7.1 and Windows 8
  • Data Structures Make Good Status Parameters
  • Changing Sub Tree Authorities In An IFS Folder

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • PHP’s Evolution On The IBM i Platform Continues
  • Protecting IBM i Data For The AI Revolution
  • Guru: Two More Small But Handy RPG Enhancements: LIKE(*EXT) And %LOOKUPNE
  • IBM i Integration Gives Costco Shoppers Real-Time Inventory
  • IBM i PTF Guide, Volume 28, Number 33
  • IBM “Price Harmonization” Increases For Power Systems Coming In The New Year
  • Code For i Continues To Grow. Here Is What’s New
  • PowerSC: Your One-Stop Shop For Security On IBM i – And Beyond
  • As I See It: You’ve Been Flocked
  • IBM i PTF Guide, Volume 28, Number 32

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle