On Error Goto Label 
Author Message
 On Error Goto Label

Hi,

I'm new to VBScript. I need to do this:

On Error Goto ErrorHandler

<Code>

:ErrorHandler
<Error handling code>

How does VBScript do this?

Thanks,
mark.



Sun, 23 Feb 2003 06:17:58 GMT  
 On Error Goto Label


Quote:
> Hi,

> I'm new to VBScript. I need to do this:

> On Error Goto ErrorHandler

> <Code>

> :ErrorHandler
> <Error handling code>

> How does VBScript do this?

Create a Sub, and then invoke it like so:
(Make sure you have On Error Resume Next at the top of your script)

If Err.Number <> 0 Then
    MyErrorHandler
    <code to do something else one the sub is done>
End If

And then create a Sub to handle the Error:
Sub MyErrorHandler
<Error Handling Code>
End Sub

I use something like this for checking the space on the servers here. Some
of them don't have E$ shares- they have D$ shares... So, rather than
building an array of E$-having servers and D$-having servers, I used the If
Err.Number method like so:

Do While objTextStream.AtEndOfStream<> True
 strServer=objTextStream.ReadLine
 Set objDrive=objFileSystem.GetDrive("\\"&strServer&"\e$")
 If Err.Number<>0 Then
  Select Case Err.Number
  Case 76
   Set objDrive=objFileSystem.GetDrive("\\"&strServer&"\d$")
  End Select
 End If
<some more code>

In this case, I'm just using a Select Case instead of invoking a
subroutine...



Sun, 23 Feb 2003 06:35:40 GMT  
 On Error Goto Label
VBScript doesn't support On Error Goto

Only On Error Resume Next and On Error GoTo 0.

What I usually do is have an error handling function
called something like ProcessError that detects
Err.Number <> 0 and processes appropriately.

So, you put ProcessError after every statement you
think might fail and have it do the dirty work.

Dim blah, blah2

On Error Resume Next

Set blah = CreateObject("blah.blah")
ProcessError "Could not create blah.blah object", True

Set blah2 = blah.Open("c:\blah")
ProcessError "Could not open c:\blah", True

and so forth.

Process error would look something like this:

Function ProcessError(sMsg, bFatal)

    If Err.Number <> 0 Then
        WScript.Echo sMsg
        If bFatal Then
            WScript.Quit
        End If
    End If

End Function

In larger functions, I usually have an error string resource file
that contains all the error messages and my processError function
has the ability to throw and error without messing with the
Err object.

Function ProcessError(iRes, bForce, bFatal)

    If bForce Then
       WScript.Echo fnLoadRes(iRes)
       If bFatal Then
          WScript.Quit
       End If
    ElseIf Err.Number <> 0 Then
       WScript.Echo fnLoadRes(iRes)
       If bFatal Then
          WScript.QUit
       End If
    End If

End Function

fnLoadRes parses through a file and loads the appropriate
resource string.

The resource file looks like this:
01=Could not create blah.blah object
02=Could not open c:\blah
03=Could not find Frank
04=Could not find George

Then I have a series of constants that map to the error codes.

Const ERR_BLAH_1   = 1
Const ERR_BLAH_2   = 2
Const ERR_FRANK_1  = 3
Const ERR_GEORGE_1 = 4

Set blah = CreateObject("blah.blah")
ProcessError ERR_BLAH_1, False, True

If Not fnFind("George") Then
   ProcessError ERR_GEORGE_1, True, True
End If

-Chad


Quote:
> Hi,

> I'm new to VBScript. I need to do this:

> On Error Goto ErrorHandler

> <Code>

> :ErrorHandler
> <Error handling code>

> How does VBScript do this?

> Thanks,
> mark.



Sun, 23 Feb 2003 06:39:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. vb ignores "on error goto label"

2. vb ignores "on error goto label"

3. How to write "On Error GoTo label"

4. On Error Goto Errorhandler -- label undefined?

5. goto error (label)

6. how pass argument to a goto label

7. click on label and goto an url

8. Problem whit GOTO ----- LABEL in eMbedded Visual Basic 3.0

9. Compilation error: On Error Goto...Syntax

10. On error Goto causes error in compilation

11. Error with On Error GoTo statement

12. Error with On error Goto... Vb4 32bits

 

 
Powered by phpBB® Forum Software