• 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
    JAMS Software

    One Scheduler. IBM i, Windows, Linux, and More.

    IBM i teams trust JAMS to schedule and orchestrate jobs across every platform in their environment. Centralized visibility, cross-platform dependency management, and alerts that reach the right person before the business feels it.

    Fewer than 5% of IBM i shops run IBM i only. The rest are managing cross-platform dependencies — often without a clear picture of how they connect. JAMS draws that map, enforces those dependencies automatically, and gives your team a single place to monitor, manage, and recover when something goes wrong.

    If you are running hundreds of CL scripts and custom RPG processes, bring them as-is. JAMS runs them exactly as they do today — except now they are visible, monitored, and part of an orchestrated workflow instead of scattered across folders only one person knows about.

    No consumption-based pricing. No surprise bills when your workload spikes. You pay based on how many machines JAMS talks to — that’s it.

    Learn More → https://jamsscheduler.com/lp/ibm-i

    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

  • Oracle Dips A Toe Into IBM’s EBCDIC World
  • When Your Small IBM i Team Is Really A Team Of One
  • Guru: Putting Failure Handling In Its Place
  • Inside The Security Enhancements In ACS
  • IBM i PTF Guide, Volume 28, Number 28: A Crazy Number of Security Vulnerability Patches
  • IBM i PTF Guide, Volume 28, Number 29
  • Inside The Encryption Key Management Changes In IBM i 7.6
  • FalconStor Moved To The Blue Lagoon, And Is Poised For Growth Because Of It
  • Guru: Claude’s SQL Tip
  • Astera Makes Extracting Legacy Report Data an AI Specialty

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