TRY … CATCH statement is one of the statements used for error handling. The general syntax of the statement is as follows:
Try Clause |
A set of statements that may cause a runtime error |
Catch Clause |
A set of statements handling the occurred error |
When the program encounters the TRY keyword, it begins executing the statements specified in the TRY Clause. These can be any 4GL or SQL statements. If any exception occurs during the statements execution, the program control passes to the CATCH Clause which contains the statements that are to be executed in case of the error occurrence.
The execution of the source code below must result in a table creation. If the table already exists, an error will occur. In this case, the CATCH block opens a dialog window which allows user to chose whether to use the existing table or delete it.
TRY
CREATE TABLE works_list
(
obj_title VARCHAR(100),
object BYTE,
obj_descr VARCHAR(200),
obj_create_date DATE,
obj_info VARCHAR(200)
)
DISPLAY "Table created" AT 2,2
CATCH
LET answer = fgl_winquestion("Table already exists",
"Do you want to delete the existing one?",
"No", "Yes|No", "Question", 1)
IF answer = "Yes"
THEN DROP TABLE works_list
CREATE TABLE works_list
(
obj_title VARCHAR(100),
object BYTE,
obj_descr VARCHAR(200),
obj_create_date DATE,
obj_info VARCHAR(200)
)
DISPLAY "Table created" AT 2,2
END IF
END TRY
In the example above, if we still want to use the table after we dropped it, we need to use the CREATE TABLE statement again or use any other program execution control statements to return to the beginning of the TRY statement. Otherwise the table will be dropped, but not created, because the TRY...CATCH statement is not a loop.
If no CATCH clause is specified in the statement, the program control goes to the statement following the END TRY keywords in case of an exception.
If all the statements in the TRY clause are executed without exceptions, the program execution continues from the statement following the END TRY keywords.
It is also possible to nest a TRY statement into another TRY statement, if needed:
TRY
SELECT * INTO myrec.* FROM tab_1 WHERE col1 = 1
CATCH
TRY
DATABASE default_db
SELECT * INTO myrec.* FROM tab_1 WHERE col1 = 1
CATCH
ERROR "An error occurred during the SELECT statement execution"
END TRY
END TRY
TRY statement tracks the errors only in statements and operators specified within the TRY block. If the TRY clause invokes a programmer defined function, the errors in that function need special handling. Therefore, if an exception occurs in the function called from the TRY block, the program control won’t be passed to the CATCH clause of the calling TRY statement, unless the function body contains the WHENEVER ERROR statement with the RAISE action (see WHENEVER statement description).
The TRY clause can be compared with the WHENEVER ANY ERROR GOTO label construction. The execution of the extract given below will have the same results as that of the first example given in this section:
WHENEVER ANY ERROR GOTO catch_error
CREATE TABLE works_list
(
obj_title VARCHAR(100),
object BYTE,
obj_descr VARCHAR(200),
obj_create_date DATE,
obj_info VARCHAR(200)
)
DISPLAY "Table created" AT 2,2
GOTO no_error
LABEL catch_error:
LET answer = fgl_winquestion("Table already exists",
"Do you want to delete the existing one?",
"No", "Yes|No", "Question", 1)
IF answer = "Yes"
THEN DROP TABLE works_list
END IF
LABEL no_error:
...