Exception handling
An error may occur when an EGL-generated program acts as follows:
- Accesses a file, queue, or database
- Calls another program
- Invokes a function
- Performs an assignment, comparison, or calculation
try blocks
An EGL try block is a series of zero to many EGL statements within the delimiters try and end. An example is as follows:
if (userRequest = "A") try add record1; onException myErrorHandler(12); end endIn general, a try block allows your program to continue processing even if an error occurs.
The try block may include an onException clause, as shown earlier. That clause is invoked if one of the earlier statements in the try block fails; but in the absence of an onException clause, an error in a try block causes invocation of the first statement that immediately follows the try block.
The previous details on try blocks must be qualified. First, a try block affects processing only for errors in the following kinds of EGL statements:
- An I/O statement
- A system function
- A call statement
Processing of numeric overflows is not affected by the presence of a try block. For details on those kinds of error, see sysVar.handleOverflow.
Second, a try block has no effect on errors inside a user function (or program) that is invoked from within the try block. In the next example, if a statement fails in function myABC, the program ends immediately with an error message unless function myABC itself handles the error:
if (userRequest = "B") try myVariable = myABC(); onException myErrorHandler(12); end endThird, the program ends immediately and with an error message in the following cases:
- An error of a kind that is covered specifically by a try block occurs outside of a try block; or
- One of the following cases applies, even in a try block--
- A failure occurs at the invocation or return of a user-written function; or
- A non-numeric character is assigned to a numeric variable; or
- The system variable sysVar.handleHardIOErrors is set to zero rather than to one when a file I/O statement ends with a hard error (as described later).
The following cases are also of interest:
- If a value is divided by zero, a Java program handles the situation as a numeric overflow
- A Java program ends if a non-numeric character is assigned to a numeric variable
Note: To support the migration of programs written in VisualAge Generator and EGL 5.0, the variable sysVar.handleSysLibErrors (previously called ezereply) allows you to process some errors that occur outside of a try block. Avoid use of that variable, which is available only if you are working in VisualAge Generator compatibility mode.
Error-related system variables
EGL provides error-related system variables that are set in a try block either in response to successful events or in response to non-terminating errors. The values in those variables are available in the try block and in code that runs subsequent to the try block, and the values in most cases are restored after a converse, if any.
The EGL run time does not change the value of any error-related variables when statements run outside of a try block. Your program, however, may assign a value to an error-related variable outside of a try block.
The system variable sysVar.exceptionCode is given a value in various situations, and in all those situations one or more additional variables are also set, depending on the nature of the program's interaction with the run-time environment:
- The system variables sysVar.exceptionCode and sysVar.errorCode are both given values after any of the following kinds of statements run in a try block:
- A call statement
- An I/O statement that operates on an indexed, MQ, relative, or serial file
- An invocation of almost any system function
- The system variables sysVar.exceptionCode, sysVar.errorCode, sysVar.mqConditionCode, and sysVar.mqReasonCode are all given values after an I/O statement in a try block operates on an MQ record
- The system variable sysVar.exceptionCode is given a value after a relational database is accessed from a statement in a try block. Values are also assigned to variables in the SQL communication area (SQLCA); for details, see sysVar.sqlca.
If a non-terminating error occurs in a try block, the value of sysVar.exceptionCode is equivalent to the numeric component of the EGL error message that would be presented to the user if the error occurred outside of the try block. The values of the situation-specific variables like sysVar.errorCode and sysVar.mqConditionCode, however, are provided by the run-time system. In the absence of an error, the value of sysVar.exceptionCode and at least one of the situation-specific variables is the same: a string of eight zeroes.
An error code is assigned to sysVar.exceptionCode and sysVar.errorCode in the case of a non-terminating numeric overflow, as described in sysVar.handleOverflow; but a successful arithmetic calculation does not affect any of the error-related system variables.
Error-related system variables are also not affected by the invocation of a function other than a system function, and sysVar.errorCode (the variable affected by most system functions) is not affected by errors in these:
- sysLib.calculateChkDigitMod10
- sysLib.calculateChkDigitMod11
- strLib.concatenate
- strLib.concatenateWithSeparator
- sysLib.connectionService
- sysLib.connect
- sysLib.convert
- sysLib.disconnect
- sysLib.disconnectAll
- sysLib.purge
- sysLib.queryCurrentDatabase
- strLib.setBlankTerminator
- sysLib.setCurrentDatabase
- strLib.strLen
- sysLib.verifyChkDigitMod10
- sysLib.verifyChkDigitMod11
- sysLib.wait
When an error value is assigned to sysVar.exceptionCode, the system variable sysVar.exceptionMsg is assigned the text of the related EGL error message, and the system variable sysVar.exceptionMsgCount is assigned the number of bytes in the error message, excluding trailing blanks and nulls. When the string of eight zeroes is assigned to sysVar.exceptionCode, sysVar.exceptionMsg is assigned blanks and sysVar.exceptionMsgCount is set to zero.
I/O statements
In relation to I/O statements, an error can be hard or soft:
- A soft error is any of these--
- No record was found during an I/O operation on an SQL database table
- One of the following problems occurs in an I/O operation on an indexed, relative, or serial file:
- Duplicate record (when the external data store allows insertion of a duplicate)
- No record found
- End of file
- A hard error is any other problem; for example--
- Duplicate record (when the external data store prohibits insertion of a duplicate)
- File not found
- Communication links are not available during remote access of a data set
If a soft I/O error occurs in a try block, the generated program continues running. If a hard I/O error occurs in a try block, the consequence depends on the value of an error-related system variable:
- During access of a file, relational database, or MQSeries message queue, the following rules apply--
- If sysVar.handleHardIOErrors is set to 1, the program continues running
- If sysVar.handleHardIOErrors is set to 0, the program presents an error message, if possible, and ends
If either a hard or soft I/O error occurs outside of a try block, the generated program presents an error message, if possible, and ends.
If you are accessing DB2 directly (not through JDBC), the sqlcode for a hard error is 304, 802, or less than 0.
Error identification
You can determine what kind of error occurred in a try block by including a case or if statement inside or outside the try block, and in that statement you can test the value of various system variables. If you are responding to an I/O error and if your statement uses an EGL record, however, it is recommended that you use an elementary logical expression. Two formats of the expression are available:
recordName is IOerrorValue recordName not IOerrorValue
- recordName
- Name of the record used in the I/O operation
- IOerrorValue
- One of several I/O error values that are constant across database management systems
If you don't use the logical expressions with I/O error values and then change database management systems, you may need to modify and regenerate your program. In particular, it is recommended that you use the I/O error values to test for errors rather than the value of sysVar.sqlcode or sysVar.sqlState. Those values are dependent on the underlying database implementation.
Related concepts
Compatibility with VisualAge Generator
Related reference
EGL Java run-time error codes
Exception handling and status (system words)
sysVar.handleHardIOErrors
sysVar.handleOverflow
sysVar.overflowIndicator
sysVar.handleSysLibErrors
sysVar.sqlca
sysVar.sqlcode
sysVar.sqlState
sysVar.mqConditionCode
sysVar.errorCode
I/O error values
Logical expressions
EGL statements