Advanced features / Exceptions |
Use the WHENEVER instruction to define how exceptions must be handled for the rest of the module.
WHENEVER exception-class exception-action
where exception-class is one of:
{ [ANY] ERROR | [ANY] SQLERROR | NOT FOUND | WARNING }
and exception-action is one of:
{ CONTINUE | STOP | CALL function | RAISE | GOTO label }
The WHENEVER instruction defines the exception handling by associating an exception class with an exception action.
This code example shows a typical WHENEVER instruction usage:
WHENEVER ERROR CONTINUE DROP TABLE mytable -- SQL error will be ignored CREATE TABLE mytable ( k INT, c VARCHAR(20) ) WHENEVER ERROR STOP IF SQLCA.SQLCODE != 0 THEN ERROR "Could not create the table..." END IF
Exception classes ERROR and SQLERROR are synonyms (compatibility issue). The previous example could have used WHENEVER SQLERROR instead of WHENEVER ERROR.
Actions for classes ERROR, WARNING and NOT FOUND can be set independently:
WHENEVER ERROR STOP WHENEVER WARNING CONTINUE WHENEVER NOT FOUND GOTO not_found_handler ...
MAIN DEFINE x INTEGER WHENEVER ANY ERROR CALL error_handler -- WHENEVER handler takes effect LET x = 1/0 DISPLAY "Back in MAIN..." END MAIN FUNCTION error_handler() DISPLAY "error_handler: ", STATUS END FUNCTION -- output: error_handler: -1202 Back in MAIN...
MAIN DEFINE x INTEGER WHENEVER ANY ERROR CONTINUE -- WHENEVER handler takes effect LET x = 1/0 DISPLAY "WHENEVER: ", STATUS -- WHENEVER handler is hidden by TRY/CATCH block TRY LET x = 1/0 CATCH DISPLAY "CATCH : ", STATUS END TRY -- WHENEVER handler takes again effect CALL func() END MAIN FUNCTION func() DEFINE x INTEGER LET x = 1/0 DISPLAY "WHENEVER: ", STATUS END FUNCTION -- Output: WHENEVER: -1202 CATCH : -1202 WHENEVER: -1202
-- main.4gl IMPORT FGL myutils MAIN TRY -- Pass a NULL form name to get error -1110 CALL mutils.open_form(NULL) CATCH DISPLAY "Error: ", status END TRY END MAIN -- myutils.4gl FUNCTION open_form(fn) DEFINE fn STRING WHENEVER ERROR RAISE -- Propagate exceptions to caller OPEN FORM f1 FROM fn END FUNCTION