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 terminate.
The exception record can contain additional fields where appropriate. 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, take care of initializing 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
Here, 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;
end
Exceptions do not pass from function to function like
this
in V6 exception compatibility mode, explained below.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 a second record has the same key. |
| endOfFile | For a serial, indexed, or relative record, this error indicates an attempt to read past the end of the file. |
| noRecordFound | For any record type, the error indicates an attempt 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
like this one automatically include the messageID and message fields,
just like the system exception records.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 program's v60ExceptionCompatibility property to YES. However, 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, however, 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.