Q129939: PRB: Err.Raise Doesn’t Always Generate Expected Error

Article: Q129939
Product(s): Microsoft Visual Basic for Windows
Version(s): 4.00
Operating System(s): 
Keyword(s): 
Last Modified: 11-JAN-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Standard Edition, 32-bit, for Windows, version 4.0 
- Microsoft Visual Basic Professional Edition, 16-bit, for Windows, version 4.0 
- Microsoft Visual Basic Professional Edition, 32-bit, for Windows, version 4.0 
- Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows, version 4.0 
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows, version 4.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

Using Err.Raise to generate a specific run-time error may generate a
"Application-defined or object-defined error" message rather than the expected
text that corresponds to the error number.

CAUSE
=====

Err.Raise only fills in the Source, Description, HelpFile, and HelpContext
properties of the Err object for Visual Basic for Applications errors. For other
errors, it returns "Application-defined or object-defined error."

STATUS
======

This behavior is by design.

MORE INFORMATION
================

Err.Raise is useful for generating errors when writing OLE Automation objects
because the Err object gives the programmer and user more information than is
possible if you generate errors with the Error statement.

Whether or not to use Err.Raise in code that is not related to OLE Automation
depends on if you want the additional information returned by using Err.Raise
versus the Error statment. Err.Raise is most useful for providing the user with
information on a user-defined error when writing code for an OLE Automation
object; it provides the user with information on a user-defined error. Please
see the Raise Method topic in the Visual Basic Help menu for an example on using
Err.Raise with user-defined errors in an OLE Automation application.

The following example demonstrates the information that can be obtained using
Err.Raise such as the Source property, Description property, and other
properties of the Err Object. In addition, it shows the differences in behavior
of Error.Raise when raising Visual Basic for Applications errors versus
non-Visual Basic for Application errors.

Steps to Reproduce Behavior
---------------------------

1. Start a new project in Visual Basic. Form1 is created by default.

2. Add the following code to the Form_Click event procedure:

     Private Sub Form_Load()
        On Error Resume Next
        Err.Clear
        Err.Raise 6
        'Err.Raise 381
        If Err.Number <> 0 Then
           Msg = "Error # " & Str(Err.Number) & " was generated by " & _
           Err.Source & Chr(13) & Err.Description

           MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
        End If
     End Sub

3. Run the program. The error "Overflow" is correctly generated.

4. Change the line containing "Err.Raise 6" into a comment, and uncomment the
  line containing "Err.Raise 381".

5. Run the program. Rather than the expected error "Invalid property array
  index," the error "Application-defined or object-defined error" is generated.

Visual Basic for Applications Errors
------------------------------------

The following is a list of Visual Basic for Applications errors. For a complete
list of trappable errors, please search for the "Trappable errors" topic in the
Help file.

  3      Return without GoSub
  5      Invalid procedure call
  6      Overflow
  7      Out of memory
  9      Subscript out of range
  10     Duplicate definition
  11     Division by zero
  12     Precision lost converting Variant
  13     Type mismatch
  14     Out of string space
  16     String expression too complex
  17     Can't perform requested operation
  18     User interrupt occurred
  20     Resume without error
  28     Out of stack space
  35     Sub or Function not defined
  47     Too many DLL application clients
  48     Error in loading DLL
  49     Bad DLL calling convention
  51     Internal error
  52     Bad file name or number
  53     File not found
  54     Bad file mode
  55     File already open
  57     Device I/O error
  58     File already exists
  59     Bad record length
  61     Disk full
  62     Input past end of file
  63     Bad record number
  67     Too many files
  68     Device unavailable
  70     Permission denied
  71     Disk not ready
  74     Can't rename with different drive
  75     Path/File access error
  76     Path not found
  91     Object variable not Set
  92     For loop not initialized
  93     Invalid pattern string
  94     Invalid use of Null
  423    Property or method not found
  424    Object required
  430    Class doesn't support OLE Automation
  438    Object doesn't support this property or method
  440    OLE Automation error
  445    Object doesn't support this action
  446    Object doesn't support named arguments
  447    Object doesn't support current locale setting
  448    Named argument not found
  449    Argument not optional
  450    Wrong number of arguments
  451    Object not a collection
  452    Invalid ordinal
  453    Function not defined in specified DLL
  454    Code resource not found
  455    Code resource lock error
  456    Get and Put cannot be used with arrays
  457    Can't create object

Additional query words: 4.00 vb4win vb4all

======================================================================
Keywords          :  
Technology        : kbVBSearch kbAudDeveloper kbVB400Search kbVB400 kbVB16bitSearch
Version           : 4.00
Issue type        : kbprb

=============================================================================