Product Documentation

FairCom Java & .NET Stored Procedures

Previous Topic

Next Topic

Handling Errors

FairCom DB SQL stored procedures use standard Java try/catch constructs to process exceptions.

Any errors in a FairCom DB SQL statement execution result in the creation of an DhSQLException class object. When FairCom DB 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:

getDiagnostics Method Arguments

Argument Value

Returns

RETURNED_SQLSTATE

The SQLSTATE returned by execution of the previous SQL statement.

MESSAGE_TEXT

The condition indicated by RETURNED_SQLSTATE.

CLASS_ORIGIN

Not currently used. Always returns null.

SUBCLASS_ORIGIN

Not currently used. Always returns null.

The error messages and the associated SQLSTATE and FairCom DB SQL error code values are documented in the FairCom DB SQL Reference Manual, Table B-2.

The following example shows an excerpt from a stored procedure that uses DhSQLException.getDiagnostics().

Example Exception Handling with DhSQLException.getDiagnostics()


CREATE PROCEDURE test_proc()

BEGIN

try

{

SQLIStatement insert_cust = new SQLIStatement (

"INSERT INTO customer VALUES (1,2) ");

}

catch (DhSQLException e)

{

String errstate = e.getDiagnostics (DhSQLException.RETURNED_SQLSTATE) ;

String errmesg = e.getDiagnostics (DhSQLException.MESSAGE_TEXT) ;

}

END

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.

Example Throwing Procedure-Specific Exceptions


CREATE PROCEDURE sp1_02()

BEGIN

// raising exception

DhSQLException excep = new DhSQLException(666,new String("Entered the tst02 procedure"));

if (true)

throw excep;

END

TOCIndex