7.1.2 ABORT Examples
Aborting with Dynamically Constructed Text
Aborting with a Substituted Variable Message
Aborting with a Multilingual Text
This command aborts a function and causes an error message to be displayed:
ABORT MSGTXT('Unable to locate system definition record')
Aborting with Dynamically Constructed Text
This subroutine dynamically constructs the error message that the ABORT command displays:
SUBROUTINE NAME(ABORT) PARMS((#MSGTXT1 *RECEIVED) (#MSGTXT2 *RECEIVED) (#MSGTXT3 *RECEIVED))
DEFINE FIELD(#MSGTXT1) TYPE(*CHAR) LENGTH(40) DECIMALS(0)
DEFINE FIELD(#MSGTXT2) REFFLD(#MSGTXT1)
DEFINE FIELD(#MSGTXT3) REFFLD(#MSGTXT1)
DEFINE FIELD(#MSGDTA) TYPE(*CHAR) LENGTH(132) DECIMALS(0)
USE BUILTIN(BCONCAT) WITH_ARGS(#MSGTXT1 #MSGTXT2 #MSGTXT3) TO_GET(#MSGDTA)
ABORT MSGID(DCM9899) MSGF(DC@M01) MSGDTA(#MSGDTA)
ENDROUTINE
It may be used in fatal error situations like this:
EXECUTE SUBROUTINE(ABORT) WITH_PARMS('Employee' #EMPNO 'not found')
Or this:
EXECUTE SUBROUTINE(ABORT) WITH_PARMS(#DEPTMENT 'is invalid' *BLANKS)
Aborting with a Substituted Variable Message
The aborting message wording can also be defined in a message file and the details substituted as variables at the time of the fatal error. A message file, e.g. MYMSGF is created and a message definition with an ID of MSG0001 is added to the file. The First Level Message Text is 'Employee &1 &2 &3 is not valid for this tax operation because their salary of &4 is too high.' Then the Message Data Field Formats are defined like this:
- *CHAR length 5
- *CHAR length 20
- *CHAR length 20
- *DEC length 11 decimals 2
Then, the abort is given as:
DEFINE FIELD(#SALRY_CAP) REFFLD(#SALARY) EDIT_CODE(3) DEFAULT(0)
REQUEST FIELDS(#SALRY_CAP)
SELECT FIELDS(#EMPNO #GIVENAME #SURNAME #SALARY) FROM_FILE(PSLMST)
IF COND('#SALARY > #SALRY_CAP')
ABORT MSGID(MSG0001) MSGF(MYMSGF) MSGDTA(#EMPNO #GIVENAME #SURNAME #SALARY)
ENDIF
ENDSELECT
MESSAGE MSGTXT('All Employees are OK') TYPE(*WINDOW) LOCATE(*MIDDLE)
Aborting with a Multilingual Text
In multilingual applications you sometimes need to issue fatal error messages that contain *MTXT variables as their message text. This subroutine shows a way of doing this.
SUBROUTINE NAME(ABORT) PARMS((#MSGDTA *RECEIVED))
DEFINE FIELD(#MSGDTA) TYPE(*CHAR) LENGTH(132) DECIMALS(0)
ABORT MSGID(DCM9899) MSGF(DC@M01) MSGDTA(#MSGDTA)
ENDROUTINE
It may be used in fatal error situations like this:
EXECUTE SUBROUTINE(ABORT) WITH_PARMS(*MTXTABORT_MESSAGE_1)
Or like this:
EXECUTE SUBROUTINE(ABORT) WITH_PARMS(*MTXTABORT_EMPTY_FILE)
The execution of an ABORT command in a called function can be detected and trapped by the calling function in this way:
CALL PROCESS(*DIRECT) FUNCTION(MYFUNC) IF_ERROR(ERR)
RETURN
ERR: MESSAGE MSGTXT('MYFUNC has ended with in error') TYPE(*WINDOW)
RETURN
If the function MYFUNC fails, control is passed to the ERR label (note that the IF_ERROR parameter logic may be triggered for many reasons other than the execution of a ABORT command).