Asserting the Ostensibly Unassertable
May 25, 2011 Hey, Ted
Assertions are great for validating conditions that can be tested with IF, DOW, SELECT, etc., but what about other types of conditions? For instance, can I use an assertion to test that a variable contains valid decimal data? –A Nice Lady (I’m sorry. A nice lady asked me this question in person and I forgot her name.) Yes, it is possible to check for conditions that one cannot test with conditional OPCODES such as IF, but you must use a different method. When something goes amiss, the system loads a five-digit, zoned-decimal status code with a non-zero value. You can access this status through file and program status data structures, but that is so 20th century. The modern way is to use the %STATUS built-in function. The status for invalid decimal data is 907. You can find a complete list of status codes in the RPG reference manual. Here’s one possibility: H dftactgrp(*no) actgrp(*new) H bnddir('TOOLKITBD') D AGoodPgm pr extpgm('AGOODPGM') D inDecNbr 5p 0 D AGoodPgm pi D inDecNbr 5p 0 D DecNbr s like(inDecNbr) D Status s 5s 0 /copy prototypes,assert /copy qrpglesrc,psds /free *inlr = *on; monitor; DecNbr = inDecNbr; on-error; Status = %status; endmon; assert (Status = *zero: %trimr(psdsProcName) + ' canceled with status ' + %editc(Status: 'X')); return; Here’s another method with more specific error handling: monitor; DecNbr = inDecNbr; on-error 907; assert (*off: 'Parameter inDecNbr has invalid decimal data'); on-error; Status = %status; assert (*off: %trimr(psdsProcName) + ' canceled with status ' + %editc(Status: 'X')); endmon; In each case, the assertion will fail, immediately halting the program, if the inDecNbr parameter does not contain valid decimal data. –Ted RELATED STORY
|