Results 1 to 3 of 3

Thread: VBScript Error Logging problem when using Err.Raise

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    VBScript Error Logging problem when using Err.Raise

    Sorry about the fact this is probably quite a basic question!

    I'm playing around with error logging in VBScript and I can't get it to log an error after I use err.raise to create one. If I comment out the Err.Raise it writes to the logfile with no error information. If I comment out the 'On Error Resume Next' but leave the Err.Raise uncommented an error is generated.

    I am using VBS Edit to write the script.

    Script is:
    -------------------------------------
    Option Explicit
    On Error Resume Next

    Dim objWshShl, objFsoObject, objLogFile
    Set objWshShl = WScript.CreateObject("WScript.Shell")
    Set objFsoObject = WScript.CreateObject("Scripting.FileSystemObject")

    'Main Processing Section

    Subroutine()
    WScript.Quit()

    'Procedure Section

    Function Subroutine()
    Err.Raise(6)
    If (objFsoObject.FileExists("C:\ErrorLog.txt")) Then
    Set objLogFile = objFsoObject.OpenTextFile("C:\ErrorLog.txt", 8)
    Else
    Set objLogFile = objFsoObject.OpenTextFile("C:\ErrorLog.txt", 2, True)
    End If

    objLogFile.WriteLine "Test.vbs Error: " & Err.Number & ", Description = " & Err.Description & " , Source = " & Err.Source
    objLogFile.Close
    Err.Clear
    End Function
    ----------------------------

    Any ideas? I'm sure it's something basic I'm doing wrong.

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VBScript Error Logging problem when using Err.Raise

    I'm not sure if I can explain this clearly but it is acting exactly as expected.

    You are calling Subroutine which has an error in it. Since Subroutine doesn't have error handling in it the error immeditaly bubbles up to the calling procedure without allowing the procedure to complete. Since the calling procedure has error handling it complete without barking about it.

    Try placing On Error Resume Next inside Subroutine before calling Err.Raise and see it that does it for you.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    Re: VBScript Error Logging problem when using Err.Raise

    Hi MarkT, thanks for the reply.

    Yep - moving the On Error Resume Next to within the subroutine worked - many thanks!

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