× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




On Tue, 23 Sep 2003, Rich Duzenbury wrote:
>
> assert(ptr != NULL);
>
> If the expression is false, assert prints a message indicating where in the
> code the failure occurred, and the nature of the failure.  It also aborts
> the program.  Is there any built in assert functionality in ILE RPG?

Yes, I use this in C as well.

I don't think there's anything built-in to RPG, but it's relatively easy
to write the code by using OS/400's program message facility.

> If not, how could I best implement my own?

Below is some sample code that demonstrates what I'm talking about.  (I
often find it easier to explain what I mean in code than I do in English)

Note that if you run this, and the assertion fails, the error that the
user will see is "The call to ASSERT ending in error (C G D F)"  That's
a bit misleading, but it's a useful way to do it, because you get the
chance to run an RPG formatted dump for debugging purposes.

Then, to find out what statement it occurred at, you need to look into the
job log.  You'll see a message (in this example, either "Assertion
failed." or "You must pass at least one parm!") if you hit help on that
message, and then hit F9 to show the message details, you can see what
statement it occurred at in the "To statement" field on the screen.
It's a bit of digging to find out the statement number, but I don't know
of a better way of doing it.

So, here's the sample code... let me know what you think:

     H DFTACTGRP(*NO) OPTION(*SRCSTMT)

     D Assert          PR
     D   peAssertion                  1N   const
     D   peMsgTxt                  1024A   const varying options(*nopass)

     D Escape          PR
     D   peStackCnt                  10I 0 value
     D   peMsgTxt                  1024A   const varying

     D ptr             s               *   inz(*null)

      /free
          Assert(%parms>0: 'You must pass at least one parm!');
          Assert(ptr <> *NULL);

         *inlr = *on;  // (otherwise this wont compile)
      /end-free

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      *  Assert():  Assert that a condition must be true.
      *
      *        peAssertion = condition to assert.
      *           peMsgTxt = (optional) text of message to send
      *
      *  Program will crash with a CPF9897 if assertion is not true.
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P Assert          B                   export
     D Assert          PI
     D   peAssertion                  1N   const
     D   peMsgTxt                  1024A   const varying options(*nopass)
      /free
        if not peAssertion;
            if %parms>=2;
               Escape(2: peMsgTxt);
            else;
               Escape(2: 'Assertion Failed.');
            endif;
        endif;
      /end-free
     P                 E


      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * Escape():  Send an escape message to a given call-stack entry
      *
      *       peMsgTxt = human-readable text to send
      *     peStackCnt = call-stack entry to send message to
      *
      * This procedure does not return.
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P Escape          B                   export
     D Escape          PI
     D   peStackCnt                  10I 0 value
     D   peMsgTxt                  1024A   const varying

     D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')
     D   MessageID                    7A   Const
     D   QualMsgF                    20A   Const
     D   MsgData                   1024A   Const
     D   MsgDtaLen                   10I 0 Const
     D   MsgType                     10A   Const
     D   CallStkEnt                  10A   Const
     D   CallStkCnt                  10I 0 Const
     D   MessageKey                   4A
     D   ErrorCode                    8A

     D dsEC            DS
     D  dsECBytesP             1      4I 0 inz(0)
     D  dsECBytesA             5      8I 0 inz(0)

     D wwMsgKey        S              4A

      /free

         QMHSNDPM('CPF9897': 'QCPFMSG   *LIBL': peMsgTxt: %len(peMsgTxt):
                  '*ESCAPE':'*': peStackCnt: wwMsgKey: dsEC);

      /end-free
     P                 E

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.