Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Err.Raise confusion

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
I am working on some code that will attach a user selected file onto an email. The code is in a class module, so I want to add some error handling in case the programming passes an invalid file. I have no problem if the error is #53 File Not Found. That's the one I am anticipating. But just in case some other error occurs for some other reason, I want it to throw the error. I tried using the Err.Raise, and I moved it just above the code where I would normally process the File not Found error. I did this just so I can see what it does when I throw it. But even though the original error was 53, because I intentionally sent it a bad file, it throws error 5 instead: Invalid Procedure call or argument.

Code:
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set objTheFile = objFileSystem.getfile(FileName)
    If Err.Number = 53 Then 'File not found
        Err.Raise Number:=Err.Number, Source:=Err.Source, Description:=Err.Description, HelpFile:=Err.HelpFile, HelpContext:=Err.HelpContext
        Err.Source = "Email Engine Add_Attachment"
        MsgBox prompt:="Error number " & Err.Number & vbCrLf & "Source: " & Err.Source & vbCrLf & vbCrLf & Err.Description & vbCrLf & FileName, Buttons:=vbCritical, Title:="Email Engine Error"
        Err.Clear
        Set objFileSystem = Nothing
        Set objTheFile = Nothing
        Exit Sub
    Else
        Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    End If

Why is it throwing error number 5 and not error number 53, which is what the error really was?

-JTBorton
Another Day, Another Disaster
 
What about this ?
Code:
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objTheFile = objFileSystem.GetFile(FileName)
If Err.Number <> 0 Then
    If Err.Number = 53 Then 'File not found
        MsgBox prompt:="Error number " & Err.Number & vbCrLf & "Source: "Email Engine Add_Attachment" & vbCrLf & vbCrLf & Err.Description & vbCrLf & FileName, Buttons:=vbCritical, Title:="Email Engine Error"
    End If
    Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    Set objFileSystem = Nothing
    Set objTheFile = Nothing
    Exit Sub
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
What you are trying to do doesn't really work. You have "On Error Resume Next" set, so all errors (including ones you raise) are trapped and code execution continues without reporting the error.

If you add an "On Error Goto 0" after the GetFile that may have failed, all the Err values are reset, so that your Err.Raise is trying to raise an error, code 0, and this itself errors with an error code 5.

PHV's code will fail just like yours, for all the same reasons. A better way to do it would be to let VBA issue the error by reissuing the failing command, something like:
Code:
[blue]    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    On Error GoTo AfterGetFile
    Set objTheFile = objFileSystem.getfile("C://FileName")
AfterGetFile:
    SaveErrNumber = Err.Number
    On Error GoTo 0
    Select Case SaveErrNumber
        Case 0
            [green]' All is well[/green]
        Case 53
            [green]' Handle it as you wish[/green]
        Case Else
            Resume
    End Select[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
PHV's code will fail just like yours, for all the same reasons. "

I never, ever, thought I would see that in print. Just kidding. Just kidding.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
JTBorton,
You don't need the [tt]On Error Resume Next[/tt] in your class. By default the class will ignore the error unless you have the VBE options set to Break in Class Module.

You should be able to remove it and the routine should run the way you expect.

You can then do as TonyJollans suggested and place the error handling code in the consuming procedure.

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top