| Advanced features / Exceptions | |
Use TRY / CATCH blocks to trap runtime exceptions in a delimited code block.
TRY instruction [...] CATCH instruction [...] END TRY
Any language instruction in the TRY block will be executed until an exception is thrown. After an exception the program execution continues in the CATCH block. If no CATCH block is provided, the execution continues after END TRY.
If no exception is raised by the statements between the TRY and CATCH keywords, the instructions in the CATCH section are ignored and the program flow continues after END TRY.
TRY
SELECT COUNT(*) INTO num_cust FROM customers WHERE ord_date <= max_date
CATCH
ERROR "Error caught during SQL statement execution:", SQLCA.SQLCODE
END TRY
WHENEVER ANY ERROR GOTO catch_error
SELECT COUNT(*) INTO num_cust FROM customers WHERE ord_date <= max_date
GOTO no_error
LABEL catch_error:
WHENEVER ERROR STOP
ERROR "Error caught during SQL statement execution:", SQLCA.SQLCODE
LABEL no_error
TRY
TRY
SELECT COUNT(*) INTO num_cust FROM customers
CATCH
ERROR "Try block 2: ", SQLCA.SQLCODE
END TRY
CATCH
ERROR "Try block 1: ", SQLCA.SQLCODE
END TRY
MAIN
TRY
CALL cust_report()
CATCH
ERROR "An error occurred during report execution: ", STATUS
END TRY
END MAIN
FUNCTION cust_report()
WHENEVER ERROR RAISE
START REPORT cust_rep ...
...
END FUNCTION