SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-60
Handling Exception Conditions
Handling Exception Conditions
In SQLJ, error handling is integrated with Java exception handling. Unlike embedded
SQL programs in C or COBOL, which require SQLSTATE to be checked after each
SQL statement, SQLJ programs use error handling code outside of the main code path
in Java try and catch blocks. By using try and catch blocks, you enable methods
in an SQLJ program to anticipate, recover from, and report certain errors.
This subsection explains how to handle various error conditions in an SQLJ program:
SQL Exceptions on page 3-60
Java Exceptions on page 3-62
SQL Warnings on page 3-63
Chained Exceptions and Warnings on page 3-64
Lost Open Error (8574) on page 3-66
SQL Exceptions
An error during an SQL operation causes an SQLJ clause to throw a
java.sql.SQLException, which is a subclass of java.lang.Exception. SQL
exceptions include:
SQL/MX database errors returned through JDBC to the SQLJ run time
JDBC/MX errors returned to the SQLJ run time
SQLJ run-time errors as defined by the SQLJ standard
For information about these error messages, see the SQLJ Run-Time Errors on
page D-90 and the SQL/MX Messages Manual.
To handle SQL exceptions in an SQLJ program, import the Java class
java.sql.SQLException and use Java try and catch blocks to influence
program control when an SQL exception occurs. Within a catch block, use these
methods for error handling:
getErrorCode() to retrieve SQLCODE
getSQLState() to retrieve SQLSTATE
getMessage() to return the error message of an SQL exception
If you do not code a separate catch block for SQLException, SQL exceptions are
reported as Java exceptions, java.lang.Exception.