Handling dbDAO Errors

The dbDAO classes use exception handling to let you 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
	{
	// Make 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:

#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 form is a long integer. This error number is the same number as is returned by the Err.Number property in Visual Basic. To read the HRESULT, examine the m_hr member of the CdbException object. To get the equivalent Visual Basic error number 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. This class is associated with the CdbDBEngine object’s CdbErrors collection. It holds the last DAO errors that 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 following elements:

The number value stored in the CdbError object is in the form of an IDA rather than an HRESULT, as in the CdbException object:

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.
		}
	}

The CdbLastOLEError class can be used to get extended error information, including the error description, on the last OLE error to occur. This class can be used regardless of whether the last error to occur was a DAO or an OLE error.

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.
		}
	}