Handling SQL-NS Errors

SQL-NS

SQL-NS

Handling SQL-NS Errors

When using SQL Namespace to invoke SQL Server Enterprise Manager user interface components, an administrative application should guide users, steamlining tasks and limiting the range of possible errors. Nonetheless, errors can occur and a SQL-NS application should supply error handling code to prevent abnormal termination.

Microsoft Visual Basic®/ActiveX® script supports error traps (error handlers) created using the ON ERROR statement. SQL-NS supports the Visual Basic Err object, allowing application error handlers to respond intelligently to errors raised.

In C++ applications, check the return value of each function. Every COM function returns an HRESULT value indicating success or failure. If the HRESULT return value is not SUCCESS, create a COM error handling object like this.

void ReportError()
{
   OLECHAR szMsg[512];
   LPERRORINFO pErrorInfo;

   szMsg[0] = 0;
   if ((SUCCEEDED(GetErrorInfo(0, &pErrorInfo))) 
      && (pErrorInfo != NULL))
   {
      BSTR bstrDesc = NULL;
      BSTR bstrSource = NULL;
      
      pErrorInfo->GetDescription(&bstrDesc);
      pErrorInfo->GetSource(&bstrSource);

      if (bstrDesc && wcslen(bstrDesc))
      {
         wcscat(szMsg, L"\r\n\r\n");
         if (bstrSource && wcslen(bstrSource))
         {
            wcscat(szMsg, L"[");
            wcscat(szMsg, bstrSource);
            wcscat(szMsg, L"] - ");
         }
         wcscat(szMsg, bstrDesc);
      }
      if (bstrDesc)
         SysFreeString(bstrDesc);
      if (bstrSource)
         SysFreeString(bstrSource);
      pErrorInfo->Release();
   }
   
   MessageBoxW(NULL, szMsg, L"Error", MB_OK);

}