Microsoft KB Archive/129941

= PRB:Unexpected Results When Raise Method Propogates OLE Errors =

Article ID: 129941

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q129941



SYMPTOMS
When creating OLE Servers, you often need to propagate errors specific to the OLE Server back to the client application by using the Raise method. The valid range of Error numbers is 0 - 65535, including those defined by Visual Basic. While there is a constant (vbObjectError) from which all OLE Server errors can start, unexpected results can occur when errors are raised between vbObjectError and vbObjectError + 512.



CAUSE
Visual Basic remaps some error messages between vbObjectError and vbObjectError + 512 to standard run-time errors, which can result in unexpected behavior if user-defined error numbers are not greater then vbObjectError + 512.

Because some error numbers are re-mapped by Visual Basic to standard OLE Automation run-time errors, error trapping code that relies on values between vbObjectError and vbObjectError + 512 are never be executed.



RESOLUTION
Below is sample OLE Server that correctly uses the Raise method to generate a user-defined error > vbObjectError + 512.

Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default. Add a command button (Command1) to Form1.  Add the following code to the Command1_Click procedure: Sub Command1_Click On Error GoTo errhand

Dim clsClass1 As New Class1 clsClass1.Prop1 = -4       ' Set property to invalid value Exit Sub

errhand: If Err.Number > vbObjectError Then MsgBox prompt:="User Defined OLE Automation Error (" & _        CStr(Err.Number - vbObjectError) & "):" & _ Chr$(13) & Chr$(10) & Err.Description, _ Buttons:=vbExclamation, Title:=Err.Source Else MsgBox prompt:=Err.Description, Buttons:=vbExclamation End If  End Sub

 Insert a Class Module by choosing Class Module from the Insert Menu (ALT, I, C). Class1 is assigned to the Name property by default.  Add the following code to the Class1: Property Let Prop1(vntValue) If vntValue < 0 Then ' Note the Error Number is > the vbObjectError + 512 Err.Raise Number:=vbObjectError + 1000, Source:="Class1", _ Description:="Invalid Property Value (Valid Values are 0 - 65535)" ' Note "Invalid Property Value (Valid Values are 0 - 65535)" is a        ' custom error message End If  End Property

 Change the Error Trapping option to Break on Unhandled Errors by choosing Options form the Tools menu and selecting the Advanced Tab.

This allows the error to be trapped in code, instead of the debuger stopping on the Raise Method within the Property Procedure in Class1.</li> Press the F5 key to run the program.</li> Click the Command1 button. A Message Box appears indicating a user-defined OLE Automation error has occured.</li></ol>

Additional query words: 4.00 vb4win vb4all

Keywords: kbprb KB129941

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.