34

I'm used to programing in C#, which obviously has some pretty robust error handling. Now I'm working on a short project in VBScript. I've read about the error handling with VBscript using "On Error _______" and the Err global variable.

However, is there some way I can generate my own errors? For example if one of my functions is passed the wrong type of parameter I'd rather my script just flat out fail at that point then try to continue.

Eric Anastas
  • 21,675
  • 38
  • 142
  • 236

3 Answers3

36

While @helen's answer is correct it is a bit sparse.

It details the Err.Raise() method but misses out some key points.

From the original question
However, is there some way I can generate my own errors? For example if one of my functions is passed the wrong type of parameter I'd rather my script just flat out fail at that point then try to continue.

Err.Raise() is extremely versatile it can throw existing exceptions (as already discussed) but it can also throw completely new user-defined exceptions.

Call Err.Raise(vbObjectError + 10, "My Application", "My custom error message")

The vbObjectError (-2147221504) is an in-built VBScript constant that defines the boundary for raising user-defined errors.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    The user-defined info very helpful. – bvj Oct 14 '19 at 07:32
  • 1
    During testing I found that you should that the custom error number should be in the range replace the custom error number should be in the range 513–65535 (so not 10), it does work but the custom error message is not to be found on the ASP error object (more info: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/raise-method). – Sunib Nov 13 '20 at 09:59
  • @Sunib thanks for the reference, but honestly I've never had any issues using `vbObjectError` and any integer. But thank you for pointing out the range anyway, could be useful to somebody. – user692942 Nov 15 '20 at 01:38
36

Yes, you can. Use the Err.Raise method, e.g.:

Err.Raise 5 ' Invalid procedure call or argument

For a list of VBScript's standard error codes, see Errors (VBScript).

Helen
  • 87,344
  • 17
  • 243
  • 314
4

C# try-catch-finally

try {
    // some code
} catch( Exception e ) {
    // error handler
} finally {
    // clean up things
}

VBScript equivalent

on error resume next
' some code
if( Err.number <> 0 ) then
    ' error handler -- you can use Err.raise() here to display your own errors
    Err.clear()
else
    ' clean up things
end if
on error goto 0

For good VBScript examples, you could check the ASP Xtreme Evolution project

user692942
  • 16,398
  • 7
  • 76
  • 175
  • I think the best practice is to wrap your error handling logic in a label rather than using `On Error Resume Next`. – ChaosPandion Dec 21 '10 at 19:43
  • just to make a parallel with C# – Fabio Zendhi Nagao Dec 21 '10 at 19:46
  • 8
    @ChaosPandion On Error GoTo Label isn't supported in VBScript. – Tmdean Dec 22 '10 at 02:03
  • @Tmdean - After a while VB6 and VBScript kinda blend together. – ChaosPandion Dec 22 '10 at 13:20
  • 8
    These two codes blocks are not equivalent because a `finally` block executes even when the error occurs. Your VBScript "equivalent" should have the `'clean up things` part outside the `If` statement, and an `On Error GoTo 0` to stop the error handling. Furthermore, another difference is that the VBScript `'some code` part can only be one line long, or at least can contain only one line that could generate an error, because the *very next line, no matter what it is* will be executed when `On Error Resume Next` is active. `If 1 = 1 / 0 Then` will flow INTO the If block. – ErikE Sep 03 '12 at 04:22
  • Doesn't `on error goto 0` clear the error anyway? If so, `err.clear` is redundant. – Stephen R Oct 29 '19 at 15:28
  • What does a c# example comparison have to do with error handling in VBScript? The question isn't tagged [tag:c#], stick to the topic. – user692942 Mar 23 '22 at 13:28