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.

IBM.Data.DB2 (in IBM.Data.DB2.dll)

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


.NET Framework 2.0 3.0, 3.5, and 4.0 Syntax

[Visual Basic]
NotInheritable Public Class DB2Exception
   Inherits DbException
public sealed class DB2Exception : DbException
public __gc __sealed class DB2Exception : public DbException
class DB2Exception extends DbException


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.


[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 _
    Dim myCommand As New DB2Command(mySelectQuery, myConnection)
    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

public void ThrowDB2Exception()
   string mySelectQuery = "SELECT column1 FROM table1";
   DB2Connection myConnection =
      new DB2Connection("DATABASE=BADDB;");
   DB2Command myCommand = new DB2Command(mySelectQuery,myConnection);
   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