| Introduction | MTB Statements | File System | Compiler | Applications | Reference Guide | Index |
| Language Structure | Data Division | I/O Format Division | Procedure Division | Mnemonics |
| Overview | Statement Summary | Common Parameters | Exception Handling
|
All input/output statements in MTB allow for handling of runtime exceptions. Here are some typical examples:
Each of the above situations results in a Comet runtime exception. The EXCP=statement-label parameter is used to specify an "exception path" to follow if an exception occurs during program execution. For example:
READ (1,1000) KEY=CUSTNUM$, EXCP=9999In this example, a statement-label of 9999 is specified as the exception path for the READ statement. This means that if this statement encounters an exception when it is executed, the program will branch to statement 9999, instead of abnormally ending.
There are two exception branching parameters:
EXCP=statement-label branches to statement-label EXCPSUB=statement-label branches to subroutineIn any exception routine, the program may include statements to inquire into the nature of the exception. This is done with the Comet system variable named EXCP, which contains the number of the most recently encountered Comet exception.
See Comet exception messages for a complete list of runtime exceptions.
Following the example from above, an exception routine could look like this:
9999 IF EXCP=32 THEN PRINT (0) "KEY NOT FOUND" & GOTO 100
IF EXCP=33 THEN PRINT (0) "RECORD EXTRACTED" & GOTO 100
This routine, starting with the label 9999, checks the system
variable EXCP for two of the most common values that might be
encountered during a READ:
EXCP value Meaning ================================================================= 32 The specified key was not found in the key list 33 The specified key was extracted by another userIn an exception routine, there are several ways to deal with runtime exceptions. One way is to simply branch back to a logical starting place in the program and start over. Another is to retry the action that caused the exception. This is done with the AGAIN statement.
Another course is to instruct the program to come to an abnormal end, in the case where all possible exceptions have been tested for and handled.
For example:
9999 IF EXCP=33 THEN PRINT (0) "RECORD EXTRACTED" & AGAIN
IF EXCP=32 THEN PRINT (0) "KEY NOT FOUND" & GOTO 100
ERROR
In this exception routine, if the exception is 33, the program
will display a message that the record is extracted and try the
I/O statement again (as specified by the AGAIN statement). Note:
This method should be used with care, as it could result in and
endless loop of trying the I/O statement, testing for exception
33, trying the I/O statement, etc.
In the above routine, if exceptions 32 and 33 are not the cause, the program is instructed to take an abnormal end (the ERROR statement does this). When ERROR is encountered, the QENDITOR program will be executed and the Comet exception will be displayed on the controlling terminal.
Other exception handling statements include ERRORTO and ERRORSUB, statements that set default exception paths.