Handling errors means anticipating the kinds of problems that might occur in your program and providing a code path for each of them. If you decide not to handle errors, all but the most trivial (see "I/O errors" later in this topic) will cause your program to end.
The exception record can contain additional fields. For example, the IndexOutOfBoundsException has an additional field for indexValue, which contains the value of the array index that EGL could not process.
onException(myEx NullValueException)
try intArray[10] = 0; // this may not be initialized onException(myEx NullValueException) writeStdErr(myEx); myErrorHandler(myEx); end
The myErrorHandler() function could, for example, initialize the array and perform the assignment again.
try get next mySerialRecord onException(myEx AnyException) myErrorHandler(myEx); end
try get next mySerialRecord onException(myEx FileIOException) myErrorHandler1(myEx); onException(myEx AnyException) myErrorHandler2(myEx); end
In this example, myErrorHandler2() deals with any exception other than a FileIOException.
If EGL throws an exception but no onException block catches the exception, the function ends immediately and control returns to the function that called the function that threw the error. In this way, EGL passes the exception upward until a function catches the exception with an onException block or the exception reaches the main function. If the main function fails to catch the exception, the program ends immediately and writes the message field of the exception to the log. When an exception occurs in a remotely called program, the calling program receives an InvocationException rather than the original exception. Similarly, an exception in a service function delivers a ServiceInvocationException to the calling program.
function FuncOne(myRec serialRecordType) try FuncTwo(myRec); onException(myEx AnyException) myErrorHandler2(myEx); end end function FuncTwo(myRec serialRecordType) get next myRec; endIn V6 exception compatibility mode, exceptions do not pass from function to function in this way; for more information, see "V6 exception compatibility" in this topic.
When you are reading or writing to a file, EGL makes a distinction between hard and soft I/O errors. The following errors are considered soft, that is, not likely to cause a loss of data:
Error | Meaning |
---|---|
duplicate | For an indexed or relative record, this error indicates that a second record has the same key. |
endOfFile | For a serial, indexed, or relative record, this error indicates that an attempt was made to read past the end of the file. |
noRecordFound | For any record type, the error indicates an that an attempt was made to read a record that could not be found. |
Other errors, such as an invalid file format or a full file, are considered hard errors. A hard I/O error on an indexed, relative, or serial file throws a FileIOException. A hard I/O error on an SQL database throws an SQLException.
while(TRUE) // endless loop try get next mySerialRecord; if(mySerialRecord is endOfFile) exit while; end onException(myEx AnyException) myErrorHandler(myEx); end end
get myCustomer; if(myCustomer is noRecordFound) add myCustomer; end
try get myCustomer; end if (myCustomer is noRecordFound) add myCustomer; end
try get myCustomer; onException (myEx FileIOException) if (myCustomer is noRecordFound) add myCustomer; else myErrorHandler(myEx); end end
nullEx NullValueException{}; ... throw nullEx;
Record CustomerException type Exception customerNumber INT; end ... throw new customerException { customerNumber = custNum, message = "Illegal customer number" };Custom exceptions records such as this one automatically include the messageID and message fields, just as the system exception records do.
For compatibility with earlier versions, you can still use the error handling methods from version 6 of EGL.
You specify V6 exception mode on a program-by-program basis when you set the v60ExceptionCompatibility property of the program to YES. For best results, use the same setting for all of your programs.
try posNum = abs(myVar); onException if(sysVar.errorCode = "00000008") // invalid input myErrorHandler1(); if(sysVar.errorCode = "00000012") // cannot assign value myErrorHandler2(); else myErrorHandler3(); end
vgVar.handleSysLibraryErrors = 1; posNum = abs(myVar); if(sysVar.errorCode == "00000008") // invalid input myErrorHandler1(); else if(sysVar.errorCode == "00000012") // cannot assign myErrorHandler2(); else exit program (-1); end end
If vgVar.handleSysLibraryErrors is set to 0 (the default) and you do not use a try block to catch exceptions, any exception will cause the program to terminate.
For more information, see "Exception handling."