Handling numeric errors returned as warnings

In Adaptive Server12.0 through 12.5, numeric errors are handled by default as severity 10. A severity-level 10 message is classified as a status information message, not as an error, and its content is transferred in a SQLWarning object. The following code excerpt illustrates this processing:

static void processWarnings(SQLWarning warning) 
{
if (warning != null)
 {
  System.out.println ("\n -- Warning received -- \n");
 }//end if
  while (warning != null) 
 {
  System.out.println ("Message: " +   warning.getMessage());
  System.out.println("SQLState: " +   warning.getSQLState());
  System.out.println ("ErrorCode: " +
  warning.getErrorCode());
  System.out.println ("----------------------------");
  warning = warning.getNextWarning();
 }//end while
}//end processWarnings

When a numeric error occurs, the ResultSet object returned contains no result set data, and the relevant information concerning the error must be obtained from the SQLWarning. Therefore, in a JDBC application, the code that checks for and processes a SQLWarning should not depend on there being a result set. For example, the following code checks for and processes SQLWarning data both inside and outside the result set processing while loop:

while (rs.next()) 
{
 String value = rs.getString(1);
 System.out.println ("Fetched value: " + value);

 // Check for SQLWarning on the result set.
 processWarnings (rs.getWarnings());

}//end while

 // Check for SQLWarning on the result set.
 processWarnings (rs.getWarnings());

Here the code checks for SQLWarning even if there is no result set data (rs.next( ) is false). The following example is output for a program properly written to detect and report numeric errors. The error is a division by zero:

-- Warning received --

Message: Divide by zero occurred.
SQLState: 01012
ErrorCode: 3607