Visual Basic error handling

Errors are generated when an unexpected condition is encountered.  System Err object properties are set as follows:

Example of error handling code:

  ' Example of error handling
Public Sub T_Error(ByRef Ci As CiServer)
    ' Prepare error handling
  On Error GoTo except
    ' Do some ClearImage operations, for example
    Dim Img As CiImage
    Set Img = Ci.CreateImage
      ' Since Img is not open yet, next statement generates an error
    Img.RotateLeft
  Exit Sub
except:
  T_ShowError
End Sub

  ' Example of error processing
Public Sub T_ShowError()
  If (Err.Number > 0) Then
    Debug.Print "Error from: " & Err.Source
    Debug.Print " Description: " & Err.Description & " Number: " & Hex(Err.Number)
  End If
End Sub


C++ error handling

Using #import  "ClearImage.dll"  creates stubs function for each ClearImage COM method.  This function checks HRESULT returned by ClearImage objects and raises _com_error if error is detected.   

Frame all access to ClearImage COM object in by try/catch pairs to catch and process these errors.

#import "ClearImage.dll" no_namespace named_guids

void dump_com_error( _com_error &e )
  {
  CString sMsg;
  if (e.Description().length())
    sMsg.Format ( "=====> COM error: Code: %08lx Description: %s" ,
       e.Error(), (LPCSTR)e.Description());
  else
    sMsg.Format ( "=====> COM error: Code: %08lx Message: %s" ,
       e.Error(), (LPCSTR)e.ErrorMessage());
  cout << sMsg << endl;
  OutputDebugString(sMsg);
  }

bool T_ClearImage (ICiServerPtr &Ci)
  {
  try
    {
     // Create and open image
    ICiImagePtr Image = Ci->CreateImage();
    Image->Open (_bstr_t("MyImageFile.tif"), 1);
    // Do something with Image
    Image->Flip();
    return true;
    }
  catch (_com_error &ex)
   {
   dump_com_error(ex);
   return false;
   }
  catch (...)
   {
   // process other errors
   return false;
   }
 }

 

 


© Inlite Research, Inc. 1992-2005 All Rights Reserved.