DBException

dtl


DBException

Category: exceptions Component type: type

Description

DBException is thrown when one of the database specific classes such as DBView or DBStmt encounters an error. An exception of this type stores strings representing which method the exception was thrown in and a message describing what error occurred. DBException also captures a vector of SQL errors (if any) that occurred to trigger this exception. The call to the constructor initializes these constructs (see below). The user can extract a stringified message with all of this information from the exception using the standard what() method.

Definition

Defined in the DBException.h header file.

Example: Throwing and catching a DBException ... executing an invalid query directly using a DBStmt object

void IWillThrow()
{
   DBStmt("THIS SQL QUERY WON'T EXECUTE").Execute(); // this *definitely* will throw DBException
                                                     // will print out ODBC msg. specifying this blatant syntax error!

}

int main()
{
   try
   {
       DBConnection::GetDefaultConnection.Connect("UID=example;PWD=example;DSN=example");

       // call our method which throws
       IWillThrow();
   }
   catch (RootException &ex)
   {
       // can also say: cout << ex << endl;
       // operator<<() for RootExceptions just streams out what()
       cout << ex.what() << endl;
   }
   return 0; // won't reach here ... exception thrown above
}

Model of

Standard C++ library exception.

Public base classes

RootException

Members

Member Where defined Description
DBException(const string &meth, const string &msg, DBConnection *conn, DBStmt *stmt) DBException Constructor which takes a specific method indicating where exception is being thrown, an error message describing the problem encountered, and pointers to the DBConnection and DBStmt objects being used in the operation that threw or NULL using the following convention:
  • If the exception occurred due to a bad API call and involved no actual accesses to the database, pass NULL for both pointers.
  • If the exception occurred due to a bad ODBC call (thus accesses to the database were bad) and involved a DBConnection connxn, but no associated DBStmt, pass in &connxn for the conn parameter and NULL for stmt.
  • If the exception occurred due to a bad ODBC call (thus accesses to the database were bad) and involved a DBStmt statement, remember that there is an associated DBConnection with that DBStmt, accessible by calling statement.GetConnection(). So in this case, pass in &(statement.GetConnection()) for the conn parameter and &statement for stmt.

The constructor calls a private method ShowSQLErrors() to generate the vector of any SQL errors that triggered this exception if either pointer is non-NULL.

virtual const char* what() const throw() DBException Overrides behavior in RootException. Returns a C string describing the exception, including the method it was thrown in, the error message describing why it was thrown, any SQL errors that led to the exception being thrown, and what type of exception was thrown. Subclasses do and may override what() based on their needs. See Note [1] in RootException.
virtual const TCHAR* twhat() const throw() DBException Returns a pointer to a TCHAR describing the exception, including the method it was thrown in, the error message describing why it was thrown, and what type of exception was thrown. Subclasses do and may override twhat() based on their needs. See Note [1] in RootException. This is useful for returning unicode error messages. See Unicode documentation.
friend wostream &operator<<(wostream &o, const RootException &ex) RootException Note that this is a friend function, and not a member. Streams out ex.twhat() to o. As twhat() is virtual, the appropriate version of that method will be called for ex.
friend ostream &operator<<(ostream &o, const RootException &ex) RootException Note that this is a friend function, and not a member. Streams out ex.what() to o. As what() is virtual, the appropriate version of that method will be called for ex.
pair<string, string> GetODBCError() DBException Prints out a pair containing strings indicating the SQL state and description of the ODBC error that caused this exception to be thrown.
vector<ODBCErrorInfo> GetAllODBCErrors() DBException Returns a vector of all ODBC errors associated with this exception. Streaming out the returned objects (or converting to an STL string) stringifies the ODBC error information, which includes the SQL state, native error, and description.

Notes

None.

See also

RootException.


[DTL Home]

Copyright © 2002, Michael Gradman and Corwin Joy.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

This site written using the ORB. [The ORB]

1