The dbDAO classes use exception handling to let the developer know when an error has occurred. The CdbException class provides minimal exception handling (it only has an HRESULT member); the CdbLastOLEError and CdbError classes offer more detailed error handling.
To catch an exception for dbDAO, use the CdbException class in your catch statement:
try
{
// Some dbDAO function calls
}
catch (CdbException e)
{
// Handle the exception
}
In many cases, you'll just want your exception handling code to examine the HRESULT returned. In that case, the only thing you need is the CdbException object. The only member of CdbException is m_hr, the HRESULT. Error constants that can be returned by dbDAO member functions are defined in Dbdaoerr.h. To determine exactly what the error was, you can compare the HRESULT in CdbException with values defined in Dbdaoerr.h. For example:
#include <dbdao.h>
#include <dbdaoerr.h>
void OpenADatabase(CString stDBName)
{
CdbDBEngine dben;
CdbDatabase db;
try
{
db = dben.OpenDatabase(stDBName);
}
catch (CdbException e)
{
assert (e.m_hr == E_DAO_FileNotFound);
// Inform the user that the file specified doesn't exist
// E_DAO_FileNotFound is a constant defined in dbdaoerr.h.
}
}
DAO returns numeric error values in two forms. One is in the form of an HRESULT. This is the form familiar to OLE programmers. The other is in the form of an IDA which is the form more familiar to Visual Basic programmers who have worked with DAO. To read the HRESULT, examine the m_hr member of the CdbException object. To read the error as an IDA value, wrap the m_hr member of CdbException in a DBERR macro (defined in dbdao.h).
For more involved error handling, you may want to use the CdbError class. The CdbError class is associated with the CdbDBEngine object's CdbErrors collection. It holds the last DAO errors which have occurred. The CdbErrors collection may hold more than one error if there is more information that can be passed. For example, an ODBC error usually results in more than one error in this collection. The members of this class contain:
The number value stored in the CdbError object is in the form of an IDA rather than an HRESULT, as in the CdbException object. For example:
void OpenADatabase(CString stDBName)
{
CdbDBEngine dben;
CdbDatabase db;
try
{
db = dben.OpenDatabase(stDBName);
}
catch (CdbException e)
{
int iError, cError;
CString stError = _T("");
cError = dben.Errors.GetCount();
for (iError = 0; iError < cError; iError++)
{
stError +=
dben.Errors[iError].GetDescription();
stError +=
dben.Errors[iError].GetNumber();
stError += _T("\n");
}
//Inform the user of the error with stError.
}
}
Also used for more detailed error handling, the CdbLastOLEError class is more generic to OLE than to DAO. It allows you to get extended error information on the last OLE error to occur, including its description. Regardless of whether the last error to occur was a DAO or an OLE error, you can use this class to determine the error. For example:
void OpenADatabase(CString stDBName)
{
CdbDBEngine dben;
CdbDatabase db;
try
{
db = dben.OpenDatabase(stDBName);
}
catch (CdbException e)
{
CdbLastOLEError ex;
CString stError;
stError.Format("0x%lx : %s", e.m_hr,
(LPCTSTR)ex.GetDescription());
//Inform the user of the error with stError.
}
}