DB2 Version 10.1 for Linux, UNIX, and Windows

DB2Exception Class

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

Namespace:
IBM®.Data.DB2®
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

.NET Framework 2.0 3.0, 3.5, and 4.0 Inheritance hierarchy

System.Object
   System.Exception
      System.SystemException
         System.Runtime.InteropServices.SystemException
            System.Data.Common.DbException
               IBM.Data.DB2.DB2Exception

.NET Framework 2.0 3.0, 3.5, and 4.0 Syntax

[Visual Basic]
<Serializable>
NotInheritable Public Class DB2Exception
   Inherits DbException
[C#]
[Serializable]
public sealed class DB2Exception : DbException
[C++]
[Serializable]
public __gc __sealed class DB2Exception : public DbException
[JScript]
public
   Serializable
class DB2Exception extends DbException

Remarks

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.

Example

[Visual Basic, C#] The following example generates an DB2Exception due to a missing database, and then displays the exception.

[Visual Basic]
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#]
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");
     }
   }
}

Thread safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Version information

Last update
This topic was last updated for: IBM DB2 Version 9.5
.NET Framework version
Supported in: 2.0, 3.0, 3.5, and 4.0
IBM Data Server Client
Supported in: 8.1.2 and up