• 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:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • 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

    Watson Gets Schooled By College Students And Professors IBM i Top Concerns: Build Skills, Add High Availability, Serve Users

    Leave a Reply Cancel 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

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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