Neoview Guide to Stored Procedures in Java (R2.5)

Exception Handling
For SPJ methods that access a Neoview database, no special code is necessary for handling
exceptions. If an SQL operation fails inside an SPJ, the error message associated with the failure
is returned to the application that issues the CALL statement.
Handling Java Exceptions
If an SPJ method returns an uncaught Java exception or an uncaught chain of
java.sql.SQLException objects, Neoview SQL converts each Java exception object into an
SQL error condition, and the CALL statement fails. Each SQL error condition contains the message
text associated with one Java exception object.
If an SPJ method catches and handles exceptions itself, those exceptions do not affect SQL
processing.
User-Defined Exceptions
The SQLSTATE values 38001 to 38999 are reserved for you to define your own error conditions
that SPJ methods can return. By coding your SPJ method to throw a java.sql.SQLException
object, you cause the CALL statement to fail with a specific user-defined SQLSTATE value and
your own error message text.
If you define the SQLSTATE to be outside the range of 38001 to 38999, Neoview SQL raises
SQLSTATE 39001, external routine invocation exception.
This example uses the throw statement in the SPJ method named numMonthlyOrders() to
raise a user-defined error condition when an invalid argument value is entered for the month:
public static void numMonthlyOrders(int month,
int[] numOrders)
throws java.sql.SQLException
{
if ( month < 1 || month > 12 )
{
throw new java.sql.SQLException (
"Invalid value for month. "
+ "Retry the CALL statement using a number "
+ "from 1 to 12 to represent the month.", "38001" );
}
....
}
For more information about the numMonthlyOrders() method, see the “MONTHLYORDERS
Procedure” (page 73).
For information about specific SQL errors, see the Neoview Messages Manual, which lists the
SQLCODE, SQLSTATE, message text, and cause-effect-recovery information for all Neoview
SQL errors.
Compiling and Packaging Java Classes
On the Neoview platform, the class files of SPJ methods must be packaged in Java archive (JAR)
files. After writing an SPJ method, compile the Java source file of the SPJ method into Java
bytecode and package the Java bytecode in a JAR file. A Java method that you register as an SPJ
might need to access, either directly or indirectly, other Java classes to operate properly. Those
Java classes might include other application classes. To enable an SPJ method to refer to other
application classes, put the application classes in the same JAR file as the SPJ class. All classes
stored in the same JAR file as the SPJ class are accessible by default to the SPJ method.
After writing the SPJ method:
Handling Java Exceptions 31