Results 1 to 9 of 9

Thread: Why "Err.Raise" do this?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    GB
    Posts
    157

    Why "Err.Raise" do this?

    I have this code:
    Code:
    '=============================================
    'Saves a resource item to disk
    'Returns 0 on success, error number on failure
    '=============================================
    
    'Example Call:
    ' iRetVal = SaveResItemToDisk(101, custom, "C:\myImage.gif")
    
    Public Function SaveResItemToDisk(ByVal iResourceNum As Integer, _
        ByVal sResourceType As String, ByVal sDestFileName As String) As Long
    
        Dim bytResourceData() As Byte
        Dim iFileNumOut      As Integer
    
        On Error GoTo SaveResItemToDisk_err
    
        'Retrieve the resource contents (data) into a byte array
        bytResourceData = LoadResData(iResourceNum, sResourceType)
    
        'Get Free File Handle
        iFileNumOut = FreeFile
    
        'Open the output file
        Open sDestFileName For Binary Access Write As #iFileNumOut
       
        'NameOfFile = sDestFileName
        'Write the resource to the file
        Put #iFileNumOut, , bytResourceData
    
        'Close the file
        Close #iFileNumOut
    
        'Return 0 for success
        SaveResItemToDisk = 0
    
        Exit Function
    SaveResItemToDisk_err:
        'Return error number
        SaveResItemToDisk = Err.Number
    
    End Function
    it extracts "Zlib.dll" from "VB resources" and places it in the indicates directory. If the operation was not carried out correctly, the "SaveResItemToDisk" function would present the error number. If I assign "Err.Raise SaveResItemToDisk(Zlib, CUSTOM, ZlibPath)" directly to Err.Raise, I ALWAYS get error number "5"; if I assign it to a long variable like
    Code:
        ErrSaving = SaveResItemToDisk(Zlib, CUSTOM, ZlibPath)
       If ErrSaving <> 0 Then
          Err.Raise ErrSaving
       End If
    everything it's OK. Can anyone explain to me the reason for this behavior?
    Last edited by fabel358; May 18th, 2024 at 08:53 AM.

  2. #2
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,658

    Re: Why "Err.Raise" do this?

    No matter what you do you're raising an error.

    Dim nReturn as long

    nReturn = SaveRes...Disk(....)

    If nReturn <> 0 Then Err.Raise nReturn

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    GB
    Posts
    157

    Re: Why "Err.Raise" do this?

    I don't understand: the function returns a LONG value, so why does "Err.Raise" output error number "5", since that the returned value is "0"?

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,267

    Re: Why "Err.Raise" do this?

    Quote Originally Posted by fabel358 View Post
    I don't understand: the function returns a LONG value, so why does "Err.Raise" output error number "5", since that the returned value is "0"?
    Change your calling code to this:

    Code:
    
        ErrSaving = SaveResItemToDisk(Zlib, CUSTOM, ZlibPath)
    
       Debug.Print "The ErrSaving return value is: "; ErrSaving
    
       If ErrSaving <> 0 Then
          Err.Raise ErrSaving
       End If
    
    I bet you see a 5, not a 0.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,658

    Re: Why "Err.Raise" do this?

    Quote Originally Posted by fabel358 View Post
    I don't understand: the function returns a LONG value, so why does "Err.Raise" output error number "5", since that the returned value is "0"?
    Not sure but I suspect it's because maybe the Error object behaves differently than normal objects. So it's probably doing something weird with evaluating the function. I really don't know. I've never tried to use err.raise this way.

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,285

    Re: Why "Err.Raise" do this?

    Try this

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        On Error GoTo EH
        Err.Raise 0
    EH:
        MsgBox Err.Number & ", " & Err.Description, vbCritical
    End Sub
    You get 5, Invalid procedure call or argument which explains why you get error number 5 when your function succeeds.

    I mean, don't raise error number 0 because it's an error - you cannot raise error number 0 because this errors with error number 5 "Invalid procedure call or argument" which means "0 is not an error number you can raise" in this case.

    cheers,
    </wqw>

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    GB
    Posts
    157

    Re: Why "Err.Raise" do this?

    Quote Originally Posted by wqweto View Post
    Try this

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        On Error GoTo EH
        Err.Raise 0
    EH:
        MsgBox Err.Number & ", " & Err.Description, vbCritical
    End Sub
    You get 5, Invalid procedure call or argument which explains why you get error number 5 when your function succeeds.

    I mean, don't raise error number 0 because it's an error - you cannot raise error number 0 because this errors with error number 5 "Invalid procedure call or argument" which means "0 is not an error number you can raise" in this case.

    cheers,
    </wqw>
    Okay. I understand your interpretation, this could be the reason for this unusual behavior in my opinion.
    On the other hand, the ASC function also gives this error message (avoidable and annoying) even from BASIC v.2 of the Commodore Vic 20 and 64, due to a jump error in the machine language of the Basic interpreter.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    GB
    Posts
    157

    Re: Why "Err.Raise" do this?

    I verified that error 5 is output even if the number reported by the function is different from "0". To take the test I assigned "9" to
    Code:
    SaveResItemToDisk_err:
        'Return error number
        SaveResItemToDisk = 9 'Err.Number
    and the error reported is not "9" (Out of Range) but always "5" (Invalid procedure call or argument).

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,285

    Re: Why "Err.Raise" do this?

    Quote Originally Posted by fabel358 View Post
    I verified that error 5 is output even if the number reported by the function is different from "0". To take the test I assigned "9" to
    Code:
    SaveResItemToDisk_err:
        'Return error number
        SaveResItemToDisk = 9 'Err.Number
    and the error reported is not "9" (Out of Range) but always "5" (Invalid procedure call or argument).
    This returns 9 only if error occurs. If no error occurs then SaveResItemToDisk still returns 0 and so Err.Raise bombs with error number 5.

    Returning error number 9 works:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        On Error GoTo EH
        Err.Raise SaveResItemToDisk(0, 0, 0)
    EH:
        MsgBox Err.Number & ", " & Err.Description, vbCritical
    End Sub
    
    Public Function SaveResItemToDisk(ByVal iResourceNum As Integer, _
        ByVal sResourceType As String, ByVal sDestFileName As String) As Long
        SaveResItemToDisk = 9
    End Function
    This correctly shows 9, Subscript out of range.

    No idea what error messages BASIC v.2 of the Commodore Vic 20 and 64 was plagued with.

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width