Topics in this section describe methods to detect and handle errors encountered
during program execution.
The WHENEVER ERROR statement
Since program statements that access the database may be expected to fail occasionally (the row is locked, etc.) the WHENEVER ERROR statement can be used to handle this type of error.
Negative SQLCA.SQLCODE
The database server returns an execution code whenever an SQL statement is executed, available in SQLCA.SQLCODE. If the code is a negative number, an SQL error has occurred.
SQLERRMESSAGE
If an SQL error occurs, the SQLERRMESSAGE operator returns the error message associated with the error code. This is a character string that can be displayed to the user with the ERROR instruction.
Close and Free the Cursor
Closing and freeing the cursor when you no longer need it is good practice, especially if the modules are part of a larger program.
Error if Cursor is not Open
In the example program in this chapter, if the user selects the Next or Previous action from the MENU before he has queried, the program returns an error ("Program stopped at line .... Fetch attempted on unopened cursor").