Try OpenEdge Now
skip to main content
SQL Development
Stored Procedures and Triggers : Writing stored procedures : Using the OpenEdge SQL Java classes : Handling errors
Handling errors
OpenEdge SQL stored procedures use standard Java try/catch constructs to process exceptions.
Any errors in SQL statement execution result in the creation of a DhSQLException class object. When OpenEdge SQL detects an error in an SQL statement, it throws an exception. The stored procedure should use try/catch constructs to process such exceptions. The getDiagnostics method of the DhSQLException class object provides a mechanism to retrieve different details of the error.
The getDiagnostics method takes a single argument whose value specifies which error message detail it returns. The following table shows the explanations of the getDiagnostics error-handling options.
Table 22. getDiagnostics error-handling options
Argument value
The SQLSTATE returned by execution of the previous SQL statement
The condition indicated by RETURNED_SQLSTATE
Not currently used; always returned null
Not currently used; always returned null
The following example shows an excerpt from a stored procedure that uses DhSQLException.getDiagnostics.
{ SQLIStatement insert_cust = new SQLIStatement (
"INSERT INTO customer VALUES (1,2) ");
}catch (DhSQLException e)
{ errstate = e.getDiagnostics (DhSQLException.RETURNED_SQLSTATE) ;
errmesg = e.getDiagnostics (DhSQLException.MESSAGE_TEXT) ;
Stored procedures can also throw their own exceptions by instantiating a DhSQLException object and throwing the object when the procedure detects an error in execution. The conditions under which the procedure throws the exception object are completely dependent on the procedure.
The following example illustrates using the DhSQLException constructor to create an exception object called excep. It then throws the excep object under all conditions.
// raising exception
DhSQLException excep =
new DhSQLException(777,new String("Entered the tst02 procedure"));
if (true)
throw excep;