Ability to analyze results and knowledge of problem determination

[Note]

DB2Exception Class

The exception that is generated when a warning or error is returned by a database.

This class is created whenever an error generated by the database server. (Client-side errors are raised as standard common language runtime exceptions.) It always contains at least one instance of DB2Error.

If the severity of the error is too great, the server can close the DB2Connection. If the server does close the connection, the application will need to open a new connection.

The following Visual Basic example generates an DB2Exception due to a missing database, and then displays the exception:

Public Sub ThrowDB2Exception()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New DB2Connection _
       ("DATABASE=BADDB;")
    Dim myCommand As New DB2Command(mySelectQuery, myConnection)
    Try
        myCommand.Connection.Open()
    Catch myException As DB2Exception
        Dim i As Integer
        For i = 0 To myException.Errors.Count - 1
            MessageBox.Show("Index #" + i.ToString() + ControlChars.Cr _
               + "Message: " + myException.Errors(i).Message 
                             + ControlChars.Cr _
               + "Native: " + myException.Errors(i).NativeError.ToString() + ControlChars.Cr _
               + "Source: " + myException.Errors(i).Source + ControlChars.Cr _
               + "SQL: " + myException.Errors(i).SQLState + ControlChars.Cr)
        Next i
    End Try
End Sub
					

C# example:


public void ThrowDB2Exception()
{
   string mySelectQuery = "SELECT column1 FROM table1";
   DB2Connection myConnection =
      new DB2Connection("DATABASE=BADDB;");
   DB2Command myCommand = new DB2Command(mySelectQuery,myConnection);
   try
   {
      myCommand.Connection.Open();
   }
   catch (DB2Exception myException)
   {
      for (int i=0; i < myException.Errors.Count; i++)
     {
         MessageBox.Show("Index #" + i + "\n" +
             "Message: " + myException.Errors[i].Message + "\n" +
             "Native: " + myException.Errors[i].NativeError.ToString() + "\n" +
             "Source: " + myException.Errors[i].Source + "\n" +
             "SQL: " + myException.Errors[i].SQLState + "\n");
     }
   }
}

					

DB2Error Class

DB2Error class collects information relevant to a warning or error returned by the database.

An instance of this class is created whenever an error occurs on a database operation in your application. Each instance of DB2Error created by the DB2DataAdapter is then managed by the DB2ErrorCollection class, which in turn is created by the DB2Exception class.

If the severity of the error is too great, the server can close the DB2Connection. If the server does close the connection, the application will need to open a new connection.

The following C# example displays the properties of the DB2Error:

					
public void DisplayDB2ErrorCollection(DB2Exception myException)
{
   for (int i=0; i < myException.Errors.Count; i++)
   {
      MessageBox.Show("Index #" + i + "\n" +
             "Message: " + myException.Errors[i].Message + "\n" +
             "Native: " + myException.Errors[i].NativeError.ToString() + "\n" +
             "Source: " + myException.Errors[i].Source + "\n" +
             "SQL: " + myException.Errors[i].SQLState + "\n");
   }
}

					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz